views:

1012

answers:

7

Hi,

I have a very basic upload script, probably lifted straight off the php.net/move_upload_files function page.

move_uploaded_file() is failed because it cannot find the tmp file in the tmp folder. But I KNOW that it is being put there, but is removed before move_upload_file() can deal with it in my script. I know it is being put there since I can see a file in there when a large file is being posted to the server.

Also $_FILEScontains correct details for the file I have just uploaded.

Had anyone have any idea why the temporary file is being removed from /tmp before I have a chance to handle it?

Here is the basic code that I am using.

if(move_uploaded_file($_FILES['userfile']['tmp_name'], $upload_file))
{
    $result['error'] = 'false';
    $result['file_loc'] = $upload_file;
}
else
{
    $result['error'] = 'true';
}

The output of print_r($_FILES) looks like

[userfile] => Array
(
    [name] => switchsolo.png
    [type] => image/png
    [tmp_name] => /tmp/phpIyKRl5
    [error] => 0
    [size] => 6690
)

But /tmp/phpIyKRl5 simply isn't there.

A: 

Your form should use a tag like this:

<form method="post" enctype="multipart/form-data" action="...">

Use multiple/form-data as enctype.

Peter Stuifzand
Wouldn't $_FILES be empty if the enctype were incorrect?
Mike B
Not sure, I haven't test this. But from experience, everytime I forget enctype="mult...", I don't have any files. Checking this is easy. Use Live HTTP headers or similar and see if the file is sent.
Peter Stuifzand
Definitely have that in there.
Bowen
A: 

Are you 100% sure that the file actually is created in /tmp? If you don't have write permission (or the user the script runs as) the file wont be written to /tmp but (I'm guessing) you'll see it during the upload although it's not actually there when the upload finishes.

Edit: $_FILES['file']['error'] - Value: 7; Failed to write file to disk. Introduced in PHP 5.1.0. So check your PHP-version. If it's below 5.1 write to disk might be your problem.

xintron
100% sure, I can see it being created when I do a larger file upload, but as soon as it's complete it seems to just disappear.
Bowen
Ah, just read the rest of comment. We are running 5.2, and, for the moment, the tmp folder is 777
Bowen
A: 

You are using the return value of move_uploaded_file() the wrong way round:

if(move_uploaded_file($_FILES['userfile']['tmp_name'], $upload_file))
{
    $result['error'] = 'true';
}
else
{
    $result['error'] = 'false';
    $result['file_loc'] = $upload_file;
}
johannes
Surely the function returns TRUE if it has run successfully?!
Bowen
A: 

I've had this problem myself. In my case php was uploading to the wrong tmp folder. Instead of using the domains tmp folder (in a virtual host on plesk) it was uploading straight to the OS temporary folder.

Check the settings of your temporary folders

Nicky De Maeyer
A: 

The file is removed after the script finishes executing. If you run your script, and then check the /tmp/ folder, the file will not be there no matter what.

Joel L
+1  A: 

1) Are the post_max_size and upload_max_filesize holding higher value than the size of the file you are trying to upload?

2) Does your uploading script take longer time to execute than the value of the max_execution_time variable allows?

3) I assume your uploading script doesn't consume as much memory as the memory_limit variable allows. When the client is uploading the file to the server, then the server is probably holding some of it in memory while doing so. I'm not sure if it somehow affects the limit of the memory_limit variable in php.ini.

These variables can be changed in php.ini and/or .htaccess or with ini_set().

Hope that helps.

Kristinn Örn Sigurðsson
Every time I've had a file upload problem, it's been resolved by paying attention to 1 or 2. +1
bradym
A: 

For future reference this can also happen when Apache does not have access to the destination directory (Remember to change the ACLs !!).

Mark Stahler