Solved

Unable to get a hold of temp file on PHP file upload

Posted on 2009-05-19
5
373 Views
Last Modified: 2012-05-07
After uploading a file to my server, I can't get a hold of it with the move_uploaded_file() function.

If I do a print_f($_FILES)  after the upload, I get the following:

Array
(
    [FileUpload1_File] => Array
        (
            [name] => cat_lvl1_default_banner.gif
            [type] => image/gif
            [tmp_name] => C:\apps\temp_file_upload\phpFE5F.tmp
            [error] => 0
            [size] => 15281
        )

)

So it seems the upload was successful, no errors (0), and the temp file should be in "C:\apps\temp_file_upload\phpFE5F.tmp", but when I use:

move_uploaded_file($_FILES['FileUpload1_File']['tmp_name'], $new_location);

it fails. To debug, I check for is_writable([$new_location directory here]) and it is fine, I was also able to create a new file in this location manually by doing touch($new_location), so $new_location don't seem to be the problem.

In the other hand, when I tried is_uploaded_file($_FILES['FileUpload1_File']['tmp_name']) I get a false. So for some reason the file is "missing" from its temp location. This temp location folder has the permission set to be writable by all users, so I really don't know why the temp file is not there, or is not accessible to the is_uploaded_file() command.

I also tried copy($_FILES['FileUpload1_File']['tmp_name'], $new_location) instead of  move_uploaded_file(), but I get the following error:
copy(C:\apps\temp_file_upload\phpC221.tmp) [function.copy]: failed to open stream: No such file or directory

I'm thinking this is a Windows Vista issue, I'll try in a linux box, but does anybody know how can I fix this?
0
Comment
Question by:cabrera48
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 9

Expert Comment

by:Rurne
ID: 24427486
Check your php.ini.  If you're configured using safe_mode_exec_dir or open_basedir, you will need to add "c:\apps\temp_file_upload" to the appropriate path.

http://us2.php.net/manual/en/features.file-upload.common-pitfalls.php
http://us2.php.net/manual/en/ini.sect.safe-mode.php


If neither of those are enabled, then does this fail for any file?  You may also want to check your php.ini for post_max_size and upload_max_filesize; if these are too small, PHP may secretly be discarding the file before it reads the entire POST request, so while it has the tmp_name in the $_FILES superglobal, the file never got written to disc.

http://us2.php.net/manual/en/ini.core.php#ini.post-max-size
http://us2.php.net/manual/en/ini.core.php#ini.upload-max-filesize
0
 

Author Comment

by:cabrera48
ID: 24427640
Thanks Rurne,

But still not working. safe_mode_exec_dir and open_basedir are off and not set, the file sizes I am uploading are small: less than 20K, and post_max_size and upload_max_filesize are at least 2M and up.

I wish I could get low level error messages from PHP about the failure to write from memory to disk and the reason why, is there a way to do that?
0
 

Author Comment

by:cabrera48
ID: 24427692
Question,

When there is a file upload, and nothing gets done afterwards (meaning no move_uploaded_file() or rename() command is run on those files), how long will the temp files are normally kept in the temp folder?
0
 
LVL 9

Accepted Solution

by:
Rurne earned 500 total points
ID: 24427737
Not really.  The problem is that PHP is making a native call to your system and offloading the data.  If PHP were to detect any error, you would have seen it in the $_FILES['FileUpload1_File']['error'] value (0 corresponds to UPLOAD_ERR_OK, meaning it worked).  Essentially, Windows is reporting the I/O operation completed successfully and PHP uses that.

I'd double-check your group permissions on the folder; something simple like

<?php
ini_set('display_errors','1');
error_reporting(E_ALL);

$fh  = fopen('c:\apps\temp_file_upload\foo.txt', 'w');
fwrite($fh, 'Can I open a file in c:\apps\temp_file_upload?');
fclose($fh);
?>


And then check to see if your file was created.  


As far as your secondary question, IIRC, the behaviors for $_FILES and tmpfile() are the same: when you hit the end of execution, any temporary resources are deleted.  My suggestion above is to check that your PHP/web server system user actually does have permission.
0
 

Author Comment

by:cabrera48
ID: 24427823
Thanks for all the information,

I just figure it out, the problem is that I was relying on a tool to auto-generate HTML and database insert and update pages (CodeCharge), and in their efforts to make this easier for users, they do some sort of manipulation of the temp/ and final folder destinations for file uploads. If I manually add my own input field independent from those auto-generated by the tool it works as it should.

Sorry about all the confusion, but thanks anyways for all the help.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question