Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Jan 2006
    Posts
    189
    Thanks
    1
    Thanked 0 Times in 0 Posts

    upload file problems

    I have a form that allows you to update your profile on my site. This form contains an Image, About section, and Quote section. The problem i'm having is that once you update, the image upload input goes blank, and if you dont input another file, it gives me an error. This is the code:

    PHP Code:
    $target_path "avatars/";

    $target_path $target_path basename$_FILES['uploadedfile']['name']);

    if(isset(
    $_FILES['uploadedfile']['name']) && $_FILES["uploadedfile"]["type"]!="image/gif" && $_FILES["uploadedfile"]["type"]!="image/jpeg"){

    $err.= "You must upload either a gif or jpeg image.";

    }elseif (
    file_exists("avatars/" $_FILES["uploadedfile"]["name"])){

    $err .= "This file already exists.";

    }elseif(
    $_FILES["uploadedfile"]["size"] > 1000000)){

    $err .= "This file is too big.";

    }else{

    if(
    move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {

    mysql_query("UPDATE userinf SET pimage='".$_FILES['uploadedfile']['name']."',pabout='$about',pquote='$quote' WHERE username='".$userinf['username']."'"$db);

       
    $err .= "You have successfully updated your profile!<br>";
       
    } else{
        
    $err .= "There was an error uploading the file, please try again!";
    }


    The problem lies with:

    PHP Code:
    if(isset($_FILES['uploadedfile']['name']) && $_FILES["uploadedfile"]["type"]!="image/gif" && $_FILES["uploadedfile"]["type"]!="image/jpeg"){ 
    Is there a way that i can make it if no file is specified, to just skip the image upload?

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    sorry, I mess the if condition
    Edit:
    PHP Code:
    if(isset($_FILES['uploadedfile']['name']) && ($_FILES["uploadedfile"]["type"]!="image/gif" || $_FILES["uploadedfile"]["type"]!="image/jpeg")){ 
    a file can't be in the same time, gif and jpeg, you must use || instead of &&


    best regards
    Last edited by oesxyl; 02-26-2008 at 07:00 PM.

  • #3
    Regular Coder
    Join Date
    Jan 2006
    Posts
    189
    Thanks
    1
    Thanked 0 Times in 0 Posts
    K, changed that. Still getting the same error though

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by king2k5 View Post
    K, changed that. Still getting the same error though
    PHP Code:
    $target_path "avatars/";

    $target_path $target_path basename$_FILES['uploadedfile']['name']);

    if(!isset(
    $_FILES['uploadedfile']['name'])){
      
    $err "You must have a filename";
    }elseif(isset(
    $_FILES['uploadedfile']['name']) && $_FILES["uploadedfile"]["type"]!="image/gif" && $_FILES["uploadedfile"]["type"]!="image/jpeg"){

    $err.= "You must upload either a gif or jpeg image.";

    }elseif (
    file_exists("avatars/" $_FILES["uploadedfile"]["name"])){

    $err .= "This file already exists.";

    }elseif(
    $_FILES["uploadedfile"]["size"] > 1000000)){

    $err .= "This file is too big.";

    }else{

    if(
    move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {

    mysql_query("UPDATE userinf SET pimage='".$_FILES['uploadedfile']['name']."',pabout='$about',pquote='$quote' WHERE username='".$userinf['username']."'"$db);

       
    $err .= "You have successfully updated your profile!<br>";
       
    } else{
        
    $err .= "There was an error uploading the file, please try again!";
    }


    try this. I add a !isset before and change the first if in elseif. It's not tested but I think will work.
    Post result if not work.

    best regards
    Last edited by oesxyl; 02-26-2008 at 07:14 PM.

  • #5
    Regular Coder meth's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Thanks
    0
    Thanked 9 Times in 9 Posts
    Give this a whirl:

    PHP Code:
    <?php
    //SCRIPT VARS
    $err '';

    //process only if $_FILES isset
    if(!empty($_FILES)) {

        
    //UPLOAD VARS
        
    $target_path "avatars/";
        
    $user_fileName strtolower($_FILES['uploadedfile']['name']);
        
    $arr_formats = array('jpeg','jpg','gif');
        
    $maxFileSize 1000000;
        
    $format_ok false;
        
    $file_ok false;
        
        
    //check format
        
    foreach($arr_formats as $format) {
            
            if(
    strpos($user_fileName,$format)) $format_ok true;
            
        }
        if(!
    $format_ok$err .= 'You must upload either a gif or jpeg image.<br />';
        
        
    //check size
        
    if($_FILES["uploadedfile"]["size"] > $maxFileSize$err .= "This file is too big.<br />";
        
        
    //proceed if error free
        
    if($err == '') {
        
            
    //clean filename
            
    $new_fileName str_replace(' ','_',$user_fileName);
            
    $new_fileName ereg_replace("[^A-Za-z0-9_.]"''$new_fileName);
            
            
    //check for existing file
            
    $fileName_ok = (!file_exists($target_path.$new_fileName)) ? true false;
            
            
    //autorename if required
            
    if(!$fileName_ok) {
            
                
    $limit 1000;
                
                for(
    $i=1;$i<$limit;$i++) {
                
                    
    $new_fileName $i.'_'.$new_fileName;
                    
    $fileName_ok = (!file_exists($target_path.$new_fileName)) ? true false;
                    if(
    $fileName_ok) break;
                
                }
            
            }
            
            if(!
    $fileName_ok$err .= 'File exists, please rename your file and upload again.<br />';
        
    //end proceed if error free
        
    }
        
        
    //proceed if error free
        
    if($err == '') {
        
            
    $file_ok move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path.$new_fileName);
            
            if(
    $file_ok) {
            
                
    $file_qry mysql_query("UPDATE userinf SET pimage='$new_fileName',pabout='$about',pquote='$quote' WHERE username='".$userinf['username']."'"$db);
            
            } else {
            
                
    $err .= 'Unable to process your image upload.<br />';
            
            }
        
        
    //end proceed if error free
        
    }
    //end process if $_FILES
    }
    ?>
    Last edited by meth; 02-26-2008 at 09:05 PM. Reason: fix logic
    I do Web Design, Brisbane based.
    More time spent in PHP/MySQL Web Development.
    And Search Engine Optimisation takes up the rest of it.

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by meth View Post
    Give this a whirl:

    PHP Code:
    <?php
    //SCRIPT VARS
    $err '';

    //process only if $_FILES isset
    if(!empty($_FILES)) {

    //end process if $_FILES
    }
    ?>
    I'm just curios, why did you check for:

    PHP Code:
    if(!empty($_FILES)) { 
    best regards

  • #7
    Regular Coder meth's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Thanks
    0
    Thanked 9 Times in 9 Posts
    There's no point running the script if the array is empty.
    I do Web Design, Brisbane based.
    More time spent in PHP/MySQL Web Development.
    And Search Engine Optimisation takes up the rest of it.

  • #8
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by meth View Post
    There's no point running the script if the array is empty.
    if exists is never empty, I thought you have another reason and I don't see.

    thank you for replay and best regards

  • #9
    Regular Coder meth's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Thanks
    0
    Thanked 9 Times in 9 Posts
    Yes that's true. I code on autopilot and 9/10 times, I'll use empty() instead of isset(). Just a habit.
    I do Web Design, Brisbane based.
    More time spent in PHP/MySQL Web Development.
    And Search Engine Optimisation takes up the rest of it.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •