Solved

PHP timeout message

Posted on 2004-08-30
10
446 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now