Uploading files via POST, when php script starts to execute?

735 Views Asked by At

I have a simple situation: some files are uploading via html form and POST method and upload.php is called.

In upload.php I have some php stuff like

<?php
// line one
// blah blah

is_uploaded_file(...);

// blah blah

move_uploaded_file(...);

// blah blah

So, when this script starts to execute (reaches the 'line one')? When I click the submit button or when the file is fully uploaded? Can I call another php script and both script will be executed at the same time? What happens if the global object $FILES I use in loop in the another script is unset when move_uploaded_file() is called by upload.php?

2

There are 2 best solutions below

0
Juanga Covas On BEST ANSWER

Your "line one" gets executed once the file(s) are uploaded (or better said, processed by PHP as they come in encoded as multipart/form-data from your POST form, see here). But some of the files could fail to upload, and you'll have error information at $_FILES global array, see here, for several reasons.

If the file(s) are OK, they'll be found at a temporary directory and temporary names (hence the need to call move_uploaded_file()).

So you could have two different users (or browsers) submitting the same files at the same time but they would be processed apart (i.e. 4 files x 2 calls, you'll get 8 temporary files), if that's your question.

Also, as per your question, one PHP execution cannot unset the $_FILES array for the execution of the other, same script. Another story is if you end writing the same path and filenames when calling move_uploaded_file(), you'll end with 4 files. As a little advice, you have to better understand the flow of an http request to the webserver (a POST with files in your case), that is then processed by your PHP script which outputs a response to the webserver that is sent back to the client (the browser).

1
dognose On

Yes, you can upload multiple files at the same time, but be aware of "session blocking". If your upload section is behind a login or something, you'll most likely call session_start() at the beginning of the fileupload to verify the user is logged in.

In this case, the session might be blocked by the upload script, until the upload is processed. (i.e. moved internally)

So - after verifying the session, but before starting the actual file move - you might want to call session_write_close(); to release the session and further scripts can proceed with using the session data.

This has a larger impact on file-downloads (if a getFile.php is used), but could save a few seconds of waiting time for uploads as well.