Forum Moderators: coopster

Message Too Old, No Replies

new question / problem

         

RogueDogg

1:03 am on May 18, 2006 (gmt 0)

10+ Year Member



This is the error I'm getting when running my upload script:

Warning: move_uploaded_file(/upload/csv.csv): failed to open stream: No such file or directory in /home/roguedog/public_html/upload_file.php on line 6

Warning: move_uploaded_file(): Unable to move '/tmp/phpUP3Dxe' to '/upload/csv.csv' in /home/roguedog/public_html/upload_file.php on line 6
Could not upload file.

Here's the script:

<?
if(userfile=="none") {
echo "No file specified";
exit;
}
if (move_uploaded_file($userfile,"/upload/".$userfile_name)) {
echo "Your file has been uploaded.";
}else{
echo "Could not upload file.";
}
?>

I've tried chmod the directory permissions, I've tried moving the files to another directory...same errors...any idea what it's puking on?

Steerpike

3:45 am on May 18, 2006 (gmt 0)

10+ Year Member



Please tell me the name of the <file> from the original form page and I can rewrite what you have for you.

madpenguin2

3:46 am on May 18, 2006 (gmt 0)

10+ Year Member



Try giving the move_uploaded_file an absolute path (/var/www/html/upload). Right now, you're using a relative path. Depending where the script is running (in a subdirectory?) it may be trying to move the file to a directory that doesn't exist!

Steerpike

3:50 am on May 18, 2006 (gmt 0)

10+ Year Member



That's one of the things wrong with what you're trying to do. Unfortunately there are also a few other issues...such as you have the arguments for move_uploaded_file reversed and your original userfile == "" should be $userfile == "".

Seriously, I have written file upload stuff soooooooo many times I feel I could debug this in my sleep if you give me the original $_GET or $_POST variable to query to get the file itslf.

RogueDogg

3:55 am on May 18, 2006 (gmt 0)

10+ Year Member



Here's everything I have put together so far to make it work:

html form:


<html>
<head>
<title>File Upload</title>
</head>

<body>
<h2>Upload</h2>
<form enctype="multipart/form-data" action="upload_file.php" method="post">
<input type="hidden" name="max_file_size" value="2000000">
Filename:
<input name="userfile" type="file">
<br>
<br>
<input type="submit" value="Send">
</form>
</body>
</html>

php script:


<?
if(userfile=="none") {
echo "No file specified";
exit;
}
if (move_uploaded_file($userfile,"/upload/".$userfile_name)) {
echo "Your file has been uploaded.";
}else{
echo "Could not upload file.";
}
?>

I have both files located in a directory on my server /public_html/upload
and I have tried them in the root directory, both times with the same error as previously posted. Thanks for your help all...I'm pulling my last few hairs out of my head on this one. ( nooby here remember ) :)

Steerpike

4:11 am on May 18, 2006 (gmt 0)

10+ Year Member



Your upload_file.php page needs to read as follows:

<?
$userfile = $_POST['userfile']['name'];
if(!$userfile)
{
echo "There was no file supplied.<br>";
exit;
}
$where= $_SERVER['DOCUMENT_ROOT']."/uploads/";
$upload = $where.$userfile;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $upload))
{
echo "We have successfully uploaded a file.<br>";
}
else
{
echo "File upload failed for the following reasons: <br>";
print_r($_FILES);
}
?>

RogueDogg

4:30 am on May 18, 2006 (gmt 0)

10+ Year Member



ARGH....I don't know what I'm doing wrong here...

NEW ERROR: There was no file supplied

I tried putting the submit_file.html and upload_file.php in the /public_html directory and in /public_html/upload directory. Same error.

I also tried several file formats to upload incase, .csv, .txt....same error...ARGH

I really do appreciate all your help on this, that is keeping me sane... :)

Steerpike

4:58 am on May 18, 2006 (gmt 0)

10+ Year Member



Ok, let's try it with a few echo's for error checking. Change you upload_file.php to the following:


<?
$userfile = $_POST['userfile']['name'];
echo "The userfile is: ".$userfile."<br>";

$where= $_SERVER['DOCUMENT_ROOT']."/uploads/";
echo "The directory path uploading to is:".$where."<br>";
$upload = $where.$userfile;

echo "The path and file combined is: ".$upload."<br>";
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $upload))
{
echo "We have successfully uploaded a file.<br>";
}
else
{
echo "File upload failed for the following reasons: <br>";
print_r($_FILES);
}
?>

Let me know what the output for that is.

dreamcatcher

6:39 am on May 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$userfile = $_POST['userfile']['name'];

should be:

$userfile = $_FILES['userfile']['name'];

dc

Steerpike

6:44 am on May 18, 2006 (gmt 0)

10+ Year Member



hahahahahaha,
Oh, great catch Dreamcatcher. Nicely spotted. Sorry everyone: hubris indeed on my part.

dreamcatcher

7:03 am on May 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, thats not saying thats the problem, but it won`t help. ;)

Don`t worry Steerpike, sometimes two pairs of eyes are better than one.

dc

RogueDogg

11:52 am on May 18, 2006 (gmt 0)

10+ Year Member



Ok taking those changes into account, here is the error(s):

The userfile is:
The directory path uploading to is:/home/roguedog/public_html/uploads/
The path and file combined is: /home/roguedog/public_html/uploads/

Warning: move_uploaded_file(/home/roguedog/public_html/uploads/): failed to open stream: Permission denied in /home/roguedog/public_html/upload/upload_file.php on line 10

Warning: move_uploaded_file(): Unable to move '/tmp/php2KrUpt' to '/home/roguedog/public_html/uploads/' in /home/roguedog/public_html/upload/upload_file.php on line 10
File upload failed for the following reasons:
Array ( [userfile] => Array ( [name] => test.txt [type] => text/plain [tmp_name] => /tmp/php2KrUpt [error] => 0 [size] => 3824 ) )

After noticing the uploads vs upload issue, I changed the folder name to uploads and this is the error I got:

The userfile is:
The directory path uploading to is:/home/roguedog/public_html/uploads/
The path and file combined is: /home/roguedog/public_html/uploads/

Warning: move_uploaded_file(/home/roguedog/public_html/uploads/): failed to open stream: Is a directory in /home/roguedog/public_html/uploads/upload_file.php on line 10

Warning: move_uploaded_file(): Unable to move '/tmp/php5I29lE' to '/home/roguedog/public_html/uploads/' in /home/roguedog/public_html/uploads/upload_file.php on line 10
File upload failed for the following reasons:
Array ( [userfile] => Array ( [name] => test.txt [type] => text/plain [tmp_name] => /tmp/php5I29lE [error] => 0 [size] => 3824 ) )

BTW, the folder is set to 777 so that shouldn't be the issue.
It doesn't exactly seem to be catching the "userfile" name. That seems to be a sticking point cause if it didn't know the name of the file, how could it move it...maybe?

dreamcatcher

12:39 pm on May 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It seems that when you had the directory called 'upload' it was running ok apart from the fact it couldn`t be moved. Which would point to a permissions error. The change has failed to open the directory, so either its a path issue or the upload directory doesn`t exit.

There are issues with move_uploaded_file() and safe mode being enabled, but I`m not sure thats a problem here.

dc

Steerpike

12:15 am on May 19, 2006 (gmt 0)

10+ Year Member



change
$userfile = $_POST['userfile']['name'];

to
$userfile = $_FILES['userfile']['name'];

Like DC said before. It's not getting a value for the file.

RogueDogg

11:25 pm on May 19, 2006 (gmt 0)

10+ Year Member



Sorry that didn't fix it either. same error... ARGH this has me stumped ( which btw isn't hard since I'm a noob at this ).

Steerpike

3:00 am on May 22, 2006 (gmt 0)

10+ Year Member



You're uploading an empty text file.

Steerpike

3:04 am on May 22, 2006 (gmt 0)

10+ Year Member



The error is that the page isn't getting the variable containing the file you're uploading.
You can see this from the very first echo statement printed to the screen:
The userfile is:
in that there's nothing after the is: which means no file found.

The possible errors that cause this are:
the file you're uploading is empty.
the form sending the data isn't the right enctype.
you're getting the wrong variable name
you're using $_POST instead of $_FILES

Until you get some sort of response appearing in the 'userfile is: ' line nothing is going to work.

RogueDogg

10:13 pm on May 22, 2006 (gmt 0)

10+ Year Member



Ok finally found the issue. WOW what a bone headed move. I forgot to add the letter "S" in $_FILES once I fixed that it worked like a charm.

Thanks everyone for helping with this. On to the next project. :o)

coopster

3:15 pm on May 24, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Glad you got it sorted, RogueDogg. And welcome to WebmasterWorld. By the way, you will want to be wary of the files you allow your user to upload as well. There is a thread in our PHP Forum Library [webmasterworld.com] that will offer some tips on security that may be of interest to you, particularly the parts regarding file uploads:
PHP Peer Code Review [webmasterworld.com]