Solved

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

Posted on 2009-05-19
5
359 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
  • 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Hi, in this article I'm going to teach you how to run your own site, and how to let people in (without IP). I'll talk about and explain each step... :) By the way, everything in this Tutorial is completely free and legal. This article is for …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now