Forum Moderators: coopster

Message Too Old, No Replies

Uploading Images

"There was an error uploading the file."

         

AliTaylor4411

8:46 am on Aug 23, 2010 (gmt 0)

10+ Year Member



I have created the form and script but just keep getting the above error - CAN ANYONE HELP!


This is the form:

<html>
<head>
<title>Upload a File</title>
</head>
<body>
<form enctype="multipart/form-data"
action="do_uploadmatt.php" method="POST">

<p><input type="hidden" name="MAX_FILE_SIZE" value="50000">

File to Upload: <input name="image" type="file"></p>

<p> Name for Uploaded File:<input name= "filename" type="text" id="filename" value="picture.jpg"></p>

<p><input type="submit" value="Upload!"></p>
</form>
</body>
</html>

This is the script:-

<?php

//Assign the entered filename to a variable
$filename = $_POST[filename];

//Specify the upload directory - assign to a variable
$uploaddir = '************/';

//Append the filename to the end of the upload directory
$uploadfile = $uploaddir . $filename;

//If upload is successful, print confirmation message

if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile))
{

echo "File is valid, and was successfully uploaded. <br>";
echo "Its name is <a href=$filename>$filename</a>";
}
else
{
echo "There was an error uploading the file.";
}
?>

lostdreamer

3:09 pm on Aug 23, 2010 (gmt 0)

10+ Year Member



is the uploaddir writable for the script? is it a absolute or relative path ?

more error checking might be in order ;)
ie:
change:
echo "There was an error uploading the file.";
to

$uploaddir = trim($uploaddir, "/"); // remove trailing slash
if(!file_exists($uploaddir))
echo $uploaddir ." does not exist<br>";
if(!is_dir($uploaddir))
echo $uploaddir ." is not a folder<br>";
if(!is_writable($uploaddir))
echo $uploaddir ." is not writable<br>";


These 3 checks might give you more info....

rocknbil

4:53 pm on Aug 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Right, make sure it's writable, but a very common mistake in PHP is confusing a URL with a server path. An example,

$uploaddir = '/my_uploads';

will try to write the file to a directory /my_uploads at the server root, which you won't have permissions for. In a URL, "/" means example.com/, but when dealing with includes and file uploads, it means the server's root (file system, when your domain is probably at /var/www/example.com, or something like that.)

A safe bet is

$uploaddir = $_SERVER['DOCUMENT_ROOT'] . '/my_uploads';

As mentioned by Readie in this post [webmasterworld.com], document root **may** have a trailing slash and require a rtrim, I've not encountered it yet but it's obviously out there. :-)