0

As all of us know, PHP finishes the upload and the enables you to use move_uploaded_file(); before this, however, it creates a temp file and then does the job. I want to know is it possible to get the name of this uploaded file during the file upload and before populating it into $_FILES?

I want to get the upload progress, while $_SESSION and Javascript onprogress solution both suck..

  • Yes, but what are you trying to do? – Burhan Khalid Apr 13 '14 at 07:53
  • so can you say how? I need to impose some operations –  Apr 13 '14 at 07:54
  • of course while the upload is in progress –  Apr 13 '14 at 07:56
  • I believe this is platform dependant. On LA[M]P modularized systems you can inspect the `/proc//fd/*` directory. In general, you can't. Perhaps you can explain why you need this in more detail; there may be alternative ways of getting the same result. – LSerni Apr 13 '14 at 07:58
  • Now we're going somewhere. Why does session (http://www.php.net/manual/en/session.upload-progress.php) suck? Maybe you can change the interface and make it suck less, and still have a portable solution. – LSerni Apr 13 '14 at 08:03
  • possible duplicate of [Check file type (FLV) before PHP upload file in temporary folder by reading only starting 3 bytes of file](http://stackoverflow.com/questions/17340566/check-file-type-flv-before-php-upload-file-in-temporary-folder-by-reading-only) – Burhan Khalid Apr 13 '14 at 08:06

2 Answers2

2

$_FILES['file']['tmp_name']; is the filename. It is not possible in PHP (without using ugly tricks) to get the filename before the upload is finished.

To do this, you have to fallback on either Flash (uploadify) or CGI (Perl / Python / C++ / Other)

naab
  • 1,122
  • 1
  • 8
  • 25
  • I can use python (CGI), but then how is it possible? If say, perl and python do the same –  Apr 13 '14 at 07:59
  • Yes they are the same, Perl was just as example. I'm not confident enough with CGI using Python to be more specific sorry. – naab Apr 13 '14 at 08:00
  • 1
    The *process* of course knows the name of the temporary file; the problem lies in *you* accessing that information. If the platform has an API for letting you, all well and good. If it hasn't, your last recourse is some kind of hack, such as the `/proc` trick. – LSerni Apr 13 '14 at 08:01
0

A "reliable" progress bar, which seems to be your goal, will always require some sort of server and client support. In its most general and portable instance, PHP will see only the completed upload and you'll get no progress bar, but only the filled $_FILES structure.

On some platforms the information can be garnered from the system itself. For example under Linux/Apache you can inspect what temporary files Apache has opened in the /proc pseudo-filesystem, where available; so you need to put in the requisites "Linux, Apache, php5_module, /proc".

You can use a dedicated POST endpoint that does not terminate on the Web server, but on a specially crafted uploader process (I worked on a Perl script doing this years ago; I recall it used POE, and the architecture):

POST (from browser) ==> (server, proxying) ==> UPLOADER

The uploader immediately echoes a crafted GET to the server, activating
a PHP "pre-upload" page, and then might call a progress GET URL periodically
to update the upload status. When completed, it would issue a pseudo POST
to PHP "almost" as if it came from the client, sending $_POST['_FILES']
instead of $_FILES.

The $_SESSION solution is a good compromise but relies on the server not doing buffering.

A better and more "modern" solution would be to leverage the chunked upload AJAX trick and get resumable uploads, reliable progress and large file support all in one nifty package. See for example this other answer. Now you get wider server support but the solution won't work on some older browsers.

You could offer the user the choice between old-style FILE upload, Flash uploader (which bypasses all problems as it doesn't rely on the browser but on Flash code), Java FTP upload control (same thing, but sometimes with some protocol and firewall issues since it doesn't use HTTP as the container web page does), and AJAX HTML5 chunking, possibly based on browser capabilities.

I.e., a user with IE6 would see a form saying

SORRY!
Your browser does not support large file uploads and progress bar.
To send a file of no more than XXX meg,
[                    ] [Choose file...]  [ >> BEGIN UPLOAD >>> ]
Community
  • 1
  • 1
LSerni
  • 55,617
  • 10
  • 65
  • 107