Solved

PHP timeout message

Posted on 2004-08-30
10
454 Views
Last Modified: 2012-06-27
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?
0
Comment
Question by:Swafnil
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 48

Assisted Solution

by:hernst42
hernst42 earned 50 total points
ID: 11929649
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
 
LVL 4

Assisted Solution

by:sgalzin
sgalzin earned 50 total points
ID: 11929733
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
 
LVL 7

Author Comment

by:Swafnil
ID: 11929844
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 27

Assisted Solution

by:Diablo84
Diablo84 earned 100 total points
ID: 11929856
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
 
LVL 27

Assisted Solution

by:Diablo84
Diablo84 earned 100 total points
ID: 11929904
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
 
LVL 3

Assisted Solution

by:Boris Aranovich
Boris Aranovich earned 50 total points
ID: 11929937
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
 
LVL 27

Assisted Solution

by:Diablo84
Diablo84 earned 100 total points
ID: 11929958
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
 
LVL 27

Accepted Solution

by:
Diablo84 earned 100 total points
ID: 11929964
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
 
LVL 3

Assisted Solution

by:Boris Aranovich
Boris Aranovich earned 50 total points
ID: 11929967
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
 
LVL 7

Author Comment

by:Swafnil
ID: 11930208
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

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

808 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