...

View Full Version : Image Upload Code



authorandrew
11-10-2011, 08:15 PM
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?


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

mlseim
11-10-2011, 09:24 PM
You're showing us the script to upload the file,
but no scripting for INSERT or UPDATE with your database.

authorandrew
11-10-2011, 11:10 PM
Oops, I'm sorry. I thought I'd copied all of it:


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

authorandrew
11-15-2011, 05:13 PM
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

markspark100
11-15-2011, 05:48 PM
Have you check for mysql errors?


if (!mysqli_query($dbc, $query)) {
printf("Errormessage: %s\n", mysqli_error($dbc));
}

authorandrew
11-15-2011, 06:44 PM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum