PHP timeout message

Hi experts,

this question has been answered before, but I can't read the answers (I don't use EE that often, so I don't have "Premium services" access):

One of my PHP modules offers file upload but some of my client's employees are somehow stupid enough to not read the message "please upload files with a max. size of 5 MB" shown above the upload file field ... the larger uploads result in a PHP timeout error.

How could I present a custom error message instead (something like "the script didn't respond in time - please check the file size again (remember to stay below 5 MB)")?

Thank you for your help!

Sascha

P.S. Am I right in saying that there is no reliable way to check a file size BEFORE the upload begins?
LVL 7
SwafnilAsked:
Who is Participating?
 
Diablo84Connect With a Mentor Commented:
I am not aware of a way to terminate an upload and present the user with an error message half way through the process.
0
 
hernst42Connect With a Mentor Commented:
You can try adding a hidden field in the following format:

<input type="hidden" name="MAX_FILE_SIZE" value="5000000" />

If the file exceed that filit, the browser will not attach the file and uploaded file will be empty.

You can also check the size of the uploaded file before processing the file and then return an error
0
 
sgalzinConnect With a Mentor Commented:
Hi,

1. I believe you are right in saying that there is no *reliable* way to check a file size before the upload begins. The following link describes what hernst42 just said :
http://www.webclass.ru/eng/Tutorials/PHP/File_upload_with.html
However, this feature can be easily be bypassed (from what the PHP manual says here : http://uk.php.net/features.file-upload).

2. From what I've read here ( http://academ.hvcc.edu/~kantopet/php/manual/features.file-upload.html ), if you do not want to use this method, this is another way of checking the size on the server and create a custom error accordingly (the example assumes the name of your file in the HTML input is "yourInputName") :

if ( $_FILES["yourInputName"]["size"] > 5242880 ) die ( "File too big (or any custom error message)." );

Note : 5242880 is 5 Mb, in bytes, i.e. 5 * 1024 * 1024.

Good luck,

Stephane.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
SwafnilAuthor Commented:
Hi Stephane and hernst42,

MAX_FILE_SIZE somehow doesn't work with recent Netscape and Mozilla releases and these are mainly used in the clients company, but I'm also getting a file size of 0 if I try Stephane's solution - I'll check if PHP is set up correctly and will then try again.

Thank you for your troubles!
0
 
Diablo84Connect With a Mentor Commented:
Alwyas check the file size using $_FILES['userfile']['size'] rather then using a hidden field, the latter is a very bad idea.

Unless you check the referer when handling the upload using a reliable method (ie. NOT with the HTTP_REFERER but with the sessions work around) then there is nothing to stop me, for example, creating a new form with the same field names (and an increased MAX_FILE_SIZE field) and submitting it to your processing page.

As a side note this is a similar reason (one of atleast) why client side javascript validation is pointless other then for reasons of "added control".

|)iablo
0
 
Diablo84Connect With a Mentor Commented:
heres a more clear cut way for you to try the size check, note it does use the same method Stephane suggested.

$file_size = $_FILES['file_input_field_name']['size'];
$file_max = 5242880 ; //5242880 = 5mb | 1mb = 1048576 | 512kb = 524288 | 50kb = 51200 | 300kb = 307200

if ($file_size > $file_max) {
 echo "file is too big"; //or some alt error message or action
}

If the file errors as a result of being too big you may wish to use unlink to delete the temp file on the server
0
 
Boris AranovichConnect With a Mentor Senior Software EngineerCommented:
I think you're missing the point people, he sais that his script times out, due to too large upload, so no filesize checking can occure... :)
0
 
Diablo84Connect With a Mentor Commented:
in which case in the php.ini file under resource limits increase the maximum execution time (if possible)

max_execution_time = 30     ; Maximum execution time of each script, in seconds

to something like 120 for 2 minutes or more if needed

failing that do it using ini_set

ini_set("max_execution_time","120");

at the top of the script
0
 
Boris AranovichConnect With a Mentor Senior Software EngineerCommented:
hmm, on the other hand... I think got myself confused, and the file size doesn't have any affect on the script execution time.
The time it takes to upload a large file, is something that happens before the php script is executed. It's the Browsers' job to send the file, and Apache's (or IIS') job to receive the file and save it somewhere... I think...
0
 
SwafnilAuthor Commented:
Hi again,

I think Diablo84 is right in saying that there is no way to terminate the upload half way - it always ends up in the script timeout message .
I guess that my client has to live with this behavior ...

Thank you all for your help and I'll split points for your ideas.

Sascha
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.