PHP file upload from form
Posted by: clivewebb83
Posted on: 2008-05-26 14:16:00
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.
<form action="" method="post" enctype="multipart/form-data" name="photo_upload" id="photo_upload">
<label for="userfile"></label>
<input type="file" name="userfile" id="userfile">
<label for="upload"></label>
<input name="upload" type="submit" class="form_header" id="upload" value="Upload">
</form>
<?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 "<img src=$thumb width='120'>
";
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