authorandrew
11-10-2011, 07: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
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