PHP file upload from form

software development

#1

Hi there,

The following code works just fine on my testing server (localhost), but doesn’t work at all once uploaded to my dreamhost server.

<?php //set the psth $path = "images/user_photos/"; //echo $path; //set the max size of uploaded file $max_size = 500000; //echo $max_size; /* Check to see if there is a file to upload if not, then exit */ if (!isset($HTTP_POST_FILES['userfile'])) { echo "checkpoint 1"; exit; } /* check to see if the file meets our restrictions */ if (is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'])) { echo "checkpoint 2"; //check size if ($HTTP_POST_FILES['userfile']['size']>$max_size){ echo "The file is too big."; exit; } //check filetype if (($HTTP_POST_FILES['userfile']['type']=="image/gif") || ($HTTP_POST_FILES['userfile']['type']=="image/pjpeg") || ($HTTP_POST_FILES['userfile']['type']=="image/jpeg") || ($HTTP_POST_FILES['userfile']['type']=="image/png")) { //check if it's a duplicate if (file_exists($path . $HTTP_POST_FILES['userfile']['name'])){ echo "The file already exists."; exit; }

//if the file has passed all the above checks, thenk upload (copy) the file to the server.
$res = copy($HTTP_POST_FILES[‘userfile’][‘tmp_name’], $path .
$HTTP_POST_FILES[‘userfile’][‘name’]);
/* copy() method returns a boolean.
if the result of the copy() operation ($res) is false then tell user the upload failed /
if (!$res) {
echo “Upload Failed!”; exit;
} else {
/
but if the result is true, then the upload succeeded and the echo out the thumbnail
and tell the user they were successful */
$thumb = $path . $HTTP_POST_FILES[‘userfile’][‘name’];
$basefile = $HTTP_POST_FILES[‘userfile’][‘name’];
echo “Photo successfully uploaded.
”;
echo “
”;
echo $basefile;
}
} else {
echo “Wrong file type!”; exit; }
}
$my_file = $HTTP_POST_FILES[‘userfile’][‘name’];

?>

Any help would be greatly appreciated.

Thank you,

–Justin

Justin-Nicholas Toyama


#2

Per this wiki page, you will need to switch to using $_FILES if you are using PHP5.

si-blog
Max discount on any plan with promocode SCJESSEYTOTAL


#3

OMG thank you so much - I have been tearing my hair out!!!

Justin-Nicholas Toyama