Forum Moderators: coopster

Message Too Old, No Replies

Finding PHP's temp file during upload

         

ahmedtheking

4:02 pm on Jan 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is it possible for php to tell you the temp file of a file upload while it's uploading? I had the idea to read the dir for anything named php* but if others are uploading at the same time, it'll cause problems!

barns101

4:19 pm on Jan 10, 2007 (gmt 0)

10+ Year Member



$_FILES['userfile']['tmp_name'] is probably what you're looking for. See PHP: Handling file uploads [php.net] for details.

henry0

4:24 pm on Jan 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$userfile is your path to the php.ini temp dir
$userfile_name original path and file name
$userfile_size is size
$userfile_type is the type

of course userfile comes from
INPUT TYPE="FILE" NAME="userfile"
the rest of Var are global var.

So what you could do is get those info
set a delay or look at them and accept them

edit
I just was reading something I did years ago
sorry of course use $_FILE
</edit>

ahmedtheking

12:11 pm on Jan 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I know about $_FILES and so on, but that's after PHP has got the file upload to its temp location. How can I, before PHP has finished uploading the file and the page is reloaded, find the file's name?

henry0

12:32 pm on Jan 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Could you use This () [us3.php.net] to set a delay that allows for a redirect somewhere
Look at the file via some regex
Then make a decision : let it go the real tmp dir
Or delete it?

ahmedtheking

1:05 pm on Jan 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry could you explain?

coopster

1:40 pm on Jan 11, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member




I know about $_FILES and so on, but that's after PHP has got the file upload to its temp location. How can I, before PHP has finished uploading the file and the page is reloaded, find the file's name?

Which file? I mean, what if you were uploading multiple files at once? See where this is headed?

Without pulling the source code and reading about how PHP is managing each file during an upload process, you aren't going to find a simple answer for this. The real simple answer might lie in your question though ...


How can I, before PHP has finished uploading the file and the page is reloaded

After PHP has uploaded the file you have full control over the processing before the "page gets reloaded" or before you send output back to the browser. Use the information that PHP has stored in the $_FILES superglobal to handle processing and after you have completed that you can decide when and what to send back to the browser.

henry0

1:47 pm on Jan 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



At the very top of the script
First grab the file name

Pass it via a header where you want to read about it

Then php reads the second part of the script that contains the delay

From there either you are satisfied with the file in
transit, the delay expires and the file goes to the tmp dir
Or there is a problem that you might have spotted by using a regex and you delete it.

Never done it but it might do the job?

[edited by: henry0 at 1:50 pm (utc) on Jan. 11, 2007]

ahmedtheking

1:50 pm on Jan 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't quite get it, I thought that PHP goes to 'sleep' while it's uploading? Well the page you're on anyway. Even if I use AJAX to send a request to get the file name, how can I now who's file is who's?

henry0

1:59 pm on Jan 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So Coopster, could what I suggested work?

coopster

2:01 pm on Jan 11, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



As with any POST request where a server-side script is named as the action attribute, PHP is hard at work during a file upload, not sleeping.

Fisrt, I was referring to multiple files in a single upload page. One person using your form can upload just one file at a time or multiple files at once, depending on how you set up your form (for an example have a look at Uploading multiple files [php.net])

Each file being uploaded in a process like this gets it's own temporary name assigned by PHP as it is being loaded into the temporary file upload directory that you have configured (defaults to /tmp on *nix systems, see associated configuration directives for details). And it is specific to the currently running script for each individual request. That's how PHP helps you to keep from mixing up Billy's uploaded file and Suzy's uploaded file.

coopster

2:09 pm on Jan 11, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



So Coopster, could what I suggested work?

If I understand what you are saying here Henry, I don't think so, at least not in the procedural steps you have outlined. You see, by the time you are able to read the $_FILES array in your script, the HTTP POST process must have already completed. PHP populates form variables ($_POST, $_FILES, etc.) as it parses the request. So, whatever is available in $_FILES means that the file has been uploaded and is waiting for you to take action.

If you don't move or copy it out of the temporary directory PHP will delete the file from the temporary directory at the end of the request.

henry0

2:13 pm on Jan 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Learned about file process, Thanks

ahmedtheking

2:17 pm on Jan 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, I know that. My form allows a user to upload one or many at once, and yep I know that PHP has sent the files to the location, well when I tell it using move_uploaded_file(). But, how can I get the name of the tmp file BEFORE PHP starts uploading, as soon as that file has been written? Is it possible? If so, how can I make sure it's Joe's, not Suzy's?

coopster

2:21 pm on Jan 11, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You aren't going to.

Without pulling the source code and reading about how PHP is managing each file during an upload process, you aren't going to find a simple answer for this.

ahmedtheking

2:25 pm on Jan 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Lame. Well thanks for the help everyone! Oh wait, one last thing, can I tell php what to call the temp file?

coopster

2:38 pm on Jan 11, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



What is so lame about the process? What exactly is it that you are attempting to do that is hindered by this process?

barns101

2:41 pm on Jan 11, 2007 (gmt 0)

10+ Year Member



Oh wait, one last thing, can I tell php what to call the temp file?

No, you can only specify the name it should be saved as once it is moved out of the temp directory.

ahmedtheking

2:53 pm on Jan 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Lame.

coopster

2:58 pm on Jan 11, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Once again, what is so lame about the process? What exactly is it that you are attempting to do that is hindered by this process?

I'm asking because there may be another way to resolve your exact issue.

ahmedtheking

3:16 pm on Jan 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well I want to make a progress indicator for file uploading. I'll make a PHP script that periodically checks the temp file while it's being uploaded.

whoisgregg

3:18 pm on Jan 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd venture a guess and say the OP is trying to build a "file upload progress bar." That tends to be the motivation behind knowing the temp file's location.

The good news is that all it takes to build a good PHP file upload progress bar widget is a small perl bridge to manage the upload tracking. The other good news is this problem has been solved by a number of people in a number of subtly different ways. A search at your favorite SE for "php file upload progress bar" and similar should yield a variety of helpful scripts.

Also, it appears as though there is now a hook to do this natively in PHP if you are running 5.2 or greater.

Added: And I could have waited a minute and known for sure that was the reason. heheh :)

ahmedtheking

3:23 pm on Jan 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Lol, well yes I did a search, but I hate working off others' code especially when I've got my own framework. Was just wondering how I could do it myself so I could understand it too! You talk about a perl bridge, how will this work?

My server's running:

PHP 5.2.0 (cli) (built: Dec 20 2006 19:11:42)
Copyright (c) 1997-2006 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2006 Zend Technologies

Just the right version!

whoisgregg

3:37 pm on Jan 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't have the same issue working off others' code :) so I was able to grab one of those public scripts (the raditha one I believe) and use that. I read through the perl file and it handles the actual file upload and writes to a text file how many bytes have been uploaded every so often. Then AJAX calls a PHP script that reads that text file.

I don't know how to implement it natively in PHP 5.2, but two comments on this manual page [us2.php.net] by jazfresh at hotmail.com give an idea of how to go about it.

ahmedtheking

3:50 pm on Jan 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah I saw that comment, is it possible you could post the URL of the script you found?

henry0

4:07 pm on Jan 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's a good added value for a user to sight

However what happens if the file is small and the connection fast
Probably no progression bar will show?

coopster

4:20 pm on Jan 11, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Only the hooks for tracking file upload progress [webmasterworld.com] were introduced in PHP 5.2. A built-in function hasn't been developed yet. The source cited on the manual page uses an experimental PECL package called uploadprogress [pecl.php.net]. It is written in C and testifies to the amount of work required which I explained would have to be done earlier in this thread.

There are other options besides perl. PEAR has some progress bar packages [webmasterworld.com] as well.

whoisgregg

4:29 pm on Jan 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



is it possible you could post the URL of the script you found

It's not an authoritative source and would likely fall outside of what's allowed by the TOS. I've sent you a sticky.

coopster

4:38 pm on Jan 11, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



It's an open source project at sourceforge, whoisgregg. We don't mind those types of links here.

[sourceforge.net...]

ahmedtheking

4:50 pm on Jan 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cheers peeps, it seems I'm going to have to dissect these in order for them to work with what I have and I want to understand what's going on too!
This 31 message thread spans 2 pages: 31