Forum Moderators: coopster

Message Too Old, No Replies

PHP File upload not working

php file upload

         

Marrand

3:04 am on Mar 7, 2006 (gmt 0)

10+ Year Member



I need help or a pointer to a good reference for upload form scripts.

I have tried every permutation and checked every setting in my phpinfo and still can't get this simple file upload script to work, even with small files (10kb)

I just keep getting the "uh-oh - it didn't work" result, and the uploaded file does not show in the designated folder on the server.

I'm sure the form is correct, but I suspect the problem is to do with how I'm defining the destination folder, but I've tried different various ways for that and none work. The upload form is in a private admin area of the site, but I want the uploaded form to be saved to a public 'uploads' folder rather than a folder within the admin area.

Thanks for any advice.

PHPinfo
============
upload_max_filesize=50M
post_max_size = 55M
DOCUMENT_ROOT=/home/smpsadmn/public_html
HTTP_ACCEPT = image/gif, image/jpeg, image/pjpeg, etc
file_uploads = on

Marrand

*** My form is upload-form.php *****
<form enctype="multipart/form-data" action="/admin/upload-file.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="100000">
<input type="file" name="pix" size="100">
<br>
<input type="submit" name="upload" value="Upload image"></form>

**** the main file is upload-file.php ******
<?PHP
if (!isset($_POST['upload']))
{
include ("upload-form.php"); //shows form if it's not been posted
}

else
{
if($_FILES['pix']['tmp_name'] == "none")
{
echo "<b>File not successfully uploaded. Maybe check the filesize limit.</b>";
include ("upload-form.php");
exit();
}

if(!ereg("image",$_FILES['pix']['type']))
{
echo "<b>File is not an image - try another file.</b>";
include ("upload-form.php");
exit();
}

else
{
$uploadDir = '/home/smpsadmn/public_html/uploads/';
$destination = $uploadDir.basename($_FILES['pix']['name']);
$temp_file = $_FILES['pix']['tmp_name'];

if ( move_uploaded_file ($temp_file,$destination) )
{ echo '<p>The file has been successfully uploaded!</p>';
}
else
{ echo "uh-oh - it didn't work!";
}
} // end of else

} // end of else
?>

Steerpike

4:23 am on Mar 7, 2006 (gmt 0)

10+ Year Member



Does the uploads directory have the permissions to write files?

Steerpike.

Marrand

12:35 pm on Mar 7, 2006 (gmt 0)

10+ Year Member



Yep - that was all it needed. Just needed permission to write. Thanks Steerpike!

So pleased that it works now, but frustrated I spent so long trying different things. I guess this is the benefit of posting - others can see what you can't!
Now, onto the next stage of the website!