?
Solved

PHP timeout message

Posted on 2004-08-30
10
Medium Priority
?
470 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 48

Assisted Solution

by:hernst42
hernst42 earned 150 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 150 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 27

Assisted Solution

by:Diablo84
Diablo84 earned 300 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 300 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 150 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 300 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 300 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 150 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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 look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

801 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