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 6 of 6
  1. #1
    New Coder
    Join Date
    May 2010
    Posts
    79
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Image Upload Code

    Is there anything wrong with this code? The page doesn't return an error but also doesn't update the 'picture' column in my database. I tried removing the @unlink feature for $old_picture (as they will be creating a new record, not uploading an old one), but if I do this the code ends up non-functional. Anyways, what is wrong - why is the filename not being passed?

    PHP Code:
    if (!empty($picture)) { // Validate and move the uploaded picture file, if necessary
            
    list($picture_width$picture_height) = getimagesize($_FILES['picture']['tmp_name']);
            if (((
    $picture_type == 'image/gif') || ($picture_type == 'image/jpeg') || ($picture_type == 'image/pjpeg') || ($picture_type == 'image/png')) && ($picture_size 0) && ($picture_size <= PROFILE_MAXFILESIZE) && ($picture_width <= PROFILE_MAXIMGWIDTH) && ($picture_height <= PROFILE_MAXIMGHEIGHT)) {
                if (
    $_FILES['file']['error'] == 0) {
                    
    // Move the file to the target upload folder
                    
    $target FACES_UPLOADPATH basename($new_picture);
                    if (
    move_uploaded_file($_FILES['picture']['tmp_name'], $target)) {
                        
    // The new picture file move was successful, now make sure any old picture is deleted
                        
    if (!empty($old_picture) && ($old_picture != $picture)) {
                            @
    unlink(FACES_UPLOADPATH $old_picture);
                        }
                    }
                    else {
                        
    // The new picture file move failed, so delete the temporary file and set the error flag
                        
    @unlink($_FILES['picture']['tmp_name']);
                        
    $error true;
                        echo 
    '<p class="error">Sorry, there was a problem uploading your picture.</p>';
                    }
                }
            }
            else {
                
    // The new picture file is not valid, so delete the temporary file and set the error flag
                
    @unlink($_FILES['picture']['tmp_name']);
                
    $error true;
                echo 
    '<p class="error">Your picture must be a GIF, JPEG, or PNG image file no greater than ' . (PROFILE_MAXFILESIZE 1024) .
                
    ' KB and ' PROFILE_MAXIMGWIDTH 'x' PROFILE_MAXIMGHEIGHT ' pixels in size.</p>';
            }
        } 
    Andrew

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,549
    Thanks
    8
    Thanked 1,095 Times in 1,086 Posts
    You're showing us the script to upload the file,
    but no scripting for INSERT or UPDATE with your database.

  • #3
    New Coder
    Join Date
    May 2010
    Posts
    79
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Oops, I'm sorry. I thought I'd copied all of it:

    Code:
    if (isset($all_done)) { // Update the profile data in the database
    		if (!$error) {
    			$query = "INSERT INTO ff_faces (date, picture, gender, branded, user_id) " .
    			"VALUES (NOW(), '$picture', '$gender', '$branded', '$user_id')";
    
    			mysqli_query($dbc, $query);
    
    			// Confirm success with the user
    			echo '<p>Your face has been uploaded successfully.</p>';
    
    			mysqli_close($dbc);
    				echo '<div class="success alert">Success! Form has been submitted. Would you like to <a href="face-view.php">view this face</a> now?</div>';
    				exit();
    		}
    	} // End of check for form submission	
    	else {
    		$output_form = true;
    		echo '<div class="error alert">Error. You were not ready to submit this form.</div>';
    	}
    Andrew

  • #4
    New Coder
    Join Date
    May 2010
    Posts
    79
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Is there no solution to this? I don't totally understand how image uploading works in PHP so this is quite confusing to me.

    Thanks,
    Andrew

  • #5
    New Coder
    Join Date
    May 2009
    Location
    Birmingham, England
    Posts
    59
    Thanks
    5
    Thanked 6 Times in 6 Posts
    Have you check for mysql errors?
    PHP Code:
    if (!mysqli_query($dbc$query)) {
        
    printf("Errormessage: %s\n"mysqli_error($dbc));


  • #6
    New Coder
    Join Date
    May 2010
    Posts
    79
    Thanks
    8
    Thanked 0 Times in 0 Posts
    I have discovered the error after some combing of the code. I had been reusing the code, and the upload section was searching for a nonexistent variable. It's fixed now, thanks.

    Andrew


  •  

    Posting Permissions

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