Image upload issues

Discussion in 'PHP' started by audax, Oct 13, 2006.

  1. #1
    Hello! I'm having an issue making an image upload form. The form itself shows up and everything of course, just processing it, nothing shows up. The file input form name is 'photo'. Here's the processing code, maybe you guys can help?

    if(isset($_POST['submit'])){
            //Get User Input
            $username = $_SESSION['username'];
            $first=$_POST['first'];
            $last=$_POST['last'];
            $sex=$_POST['sex'];
            $description=$_POST['description'];
            
            //If image fits the filetype requirements
            if (($_FILES['photo']['type'] == "image/gif") || ($_FILES['photo']['type'] == "image/jpeg") || ($_FILES['photo']['type'] == "image/jpg") || ($_FILES['photo']['type'] == "image/png")){
                    $source = $_FILES['photo']['tmp_name'];
                    $target = "profiles/".$FILES_['photo']['name'];
                    copy($source, $target);
                    $image = "profiles/".$_FILES['photo']['name']);
                    $edit="UPDATE members SET first='$first', last='$last', sex='$sex', image='$image', description='$description' WHERE username='$username'";
                    mysql_query($edit) or die(mysql_error());
                    echo '<center><br><br><Br><img src="images/profile_header.jpg" border="0" alt="Profile"><br><br>Redirecting<br><br> <img src="images/loading.gif" border="0"><br><br>Profile Updated.  You will be redirected back to the Control Panel in 3 seconds.  If you do not wish to wait, <a href="index.php?page=members">Click Here</a>.';
                    header('Refresh: 3; url=index.php?page=members');
            } elseif (($_FILES['photo']['type'] != "image/jpeg") && ($_FILES['photo']['type'] != "image/gif") && ($_FILES['photo']['type'] != "image/jpg") && ($_FILES['photo']['type'] != "image/png")){
                    //If image does NOT fit the filetype requirements
                    echo '<center><br><br><Br><img src="images/profile_header.jpg" border="0" alt="Profile"><br><br>Wrong filetype<br><br> <img src="images/loading.gif" border="0"><br><br>Your photo could not be uploaded. Wrong Filetype ('.$_FILES['imagefile']['name'].').  Please make sure your images is a .JPG or .GIF file in order to upload it.  You will be redirected back to your Profile in 5 seconds. If you do not wish to wait, <a href="index.php?page=profile">Click Here</a>.';
                    header('Refresh: 5; url=index.php?page=profile');
            } elseif (!isset($_FILES['photo'])) {
                    //If user chose not to upload an image
                    $edit="UPDATE members SET first='$first', last='$last', sex='$sex', description='$description' WHERE username='$username'";
                    mysql_query($edit) or die(mysql_error());
                    echo '<center><br><br><Br><img src="images/profile_header.jpg" border="0" alt="Profile"><br><br>Redirecting<br><br> <img src="images/loading.gif" border="0"><br><br>Profile Updated.  You will be redirected back to the Control Panel in 3 seconds.  If you do not wish to wait, <a href="index.php?page=members">Click Here</a>.';
                    header('Refresh: 3; url=index.php?page=members');
            }
            
            } elseif(!isset($_POST['submit'])) {
            
            header("Location: index.php?page=profile");
            
            }
    PHP:
    The directory has all the write permissions allowed so it can't be that. I'm wondering if I'm missing something, or if my host simply doesn't allow for PHP uploading. The processing code worked up until I added the if() statements about the image filetype and upload, etc.
     
    audax, Oct 13, 2006 IP
  2. gigamike

    gigamike Active Member

    Messages:
    165
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #2
    hi,

    As an addition, check also or validate the file size of the image coz maybe you php directive is set to as low as 2mb and your uploading a image more than that file size.

    Another thing is check your html form tags...it should have
    <form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">--> the enctype part.

    and also check line 12

    $target = "profiles/".$FILES_['photo']['name']; notice the $FILES...

    i suggest you set the error so it will display the error to you.

    I hope this help.

    Thanks,

    gigamike
     
    gigamike, Oct 13, 2006 IP
  3. Big 'G'

    Big 'G' Member

    Messages:
    89
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    48
    #3
    Big 'G', Oct 13, 2006 IP
  4. audax

    audax Peon

    Messages:
    83
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks you both for your help. I'll give it a try and see what I can come up with.
     
    audax, Oct 13, 2006 IP
  5. audax

    audax Peon

    Messages:
    83
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Update: I got the upload portion to work with validation of proper filetypes for images (png/jpg/ etc), but what if I want to make it so that if users choose not to upload a file, it will still process the rest of the data? Is there an option to see if a "file" type field is set?

    Like, isset($_FILES['photo']) ?
    I've tried that code, but it doesn't seem to work. It acts as if the file is a wrong filetype. Any ideas? Here's the new code I've got that works for uploading a specified image, but doesn't work if the user chooses not to upload an image (blank "file" field):

            if(isset($_POST['submit'])){
                    //Get User Input
                    $username = $_SESSION['username'];
                    $first=$_POST['first'];
                    $last=$_POST['last'];
                    $sex=$_POST['sex'];
                    $description=$_POST['description'];
            
    
                    $target_path = "profiles/";
                    $target_path = $target_path . basename( $_FILES['photo']['name']);
                    $_FILES['photo']['tmp_name'];
            
                    //If image fits the filetype requirements
                    if (isset($_FILES['photo'])){
                            if (($_FILES['photo']['type'] == "image/gif") || ($_FILES['photo']['type'] == "image/jpeg") || ($_FILES['photo']['type'] == "image/jpg") || ($_FILES['photo']['type'] == "image/png")){
                            
                                    if(move_uploaded_file($_FILES['photo']['tmp_name'], $target_path)) {
                                    $edit="UPDATE members SET first='$first', last='$last', sex='$sex', image='$target_path', description='$description' WHERE username='$username'";
                                    mysql_query($edit) or die(mysql_error());
                                    echo '<center><br><br><Br><img src="images/profile_header.jpg" border="0" alt="Profile"><br><br>Redirecting<br><br> <img src="images/loading.gif" border="0"><br><br>Profile Updated.  You will be redirected back to the Control Panel in 3 seconds.  If you do not wish to wait, <a href="index.php?page=members">Click Here</a>.';
                                    header('Refresh: 3; url=index.php?page=members');
    
                                    } else {
    
                                    echo '<center>Image Upload Error!</center>';
    
                                    }
    
                            } elseif (($_FILES['photo']['type'] != "image/jpeg") && ($_FILES['photo']['type'] != "image/gif") && ($_FILES['photo']['type'] != "image/jpg") && ($_FILES['photo']['type'] != "image/png")){
    
                                    //If image does NOT fit the filetype requirements
                                    echo '<center><br><br><Br><img src="images/profile_header.jpg" border="0" alt="Profile"><br><br>Wrong filetype<br><br> <img src="images/loading.gif" border="0"><br><br>Your photo could not be uploaded. Wrong Filetype ('.$_FILES['photo']['name'].').  Please make sure your images is a .JPG or .GIF file in order to upload it.  You will be redirected back to your Profile in 5 seconds. If you do not wish to wait, <a href="index.php?page=profile">Click Here</a>.';
                                    header('Refresh: 5; url=index.php?page=profile');
                                    
                                    }
    
                            } elseif (!isset($_FILES['photo'])) {
    
                                    //If user chose not to upload an image
                                    $edit1="UPDATE members SET first='$first', last='$last', sex='$sex', description='$description' WHERE username='$username'";
                                    mysql_query($edit1) or die(mysql_error());
                                    echo '<center><br><br><Br><img src="images/profile_header.jpg" border="0" alt="Profile"><br><br>Redirecting<br><br> <img src="images/loading.gif" border="0"><br><br>Profile Updated.  You will be redirected back to the Control Panel in 3 seconds.  If you do not wish to wait, <a href="index.php?page=members">Click Here</a>.';
                                    header('Refresh: 3; url=index.php?page=members');
                            }
    PHP:
     
    audax, Oct 13, 2006 IP
  6. gigamike

    gigamike Active Member

    Messages:
    165
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #6
    hi,

    try

    if($FILES_['photo']['name']!=""){
    // execute upload
    }
    else{
    // no file to upload
    }

     
    gigamike, Oct 13, 2006 IP
  7. audax

    audax Peon

    Messages:
    83
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    You are my savior :D

    Your code worked like a charm after I changed it to $_FILES instead ;)


    Thank you so much!
     
    audax, Oct 13, 2006 IP
  8. maya786

    maya786 Peon

    Messages:
    85
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    $_FILES[] is alway's a better alternative

    :)
     
    maya786, Oct 13, 2006 IP