Forum Moderators: coopster

Message Too Old, No Replies

Possible to let the user know the image is too large

before uploading begins

         

blaketar

9:17 pm on Jan 11, 2007 (gmt 0)

10+ Year Member



Technically I do not think its possible but thought I would ask. I have a form that contains multiple image upload fields, in the description we ask that the images size be smaller than 1mb but most people simple do not know how to check this.

Typically people will load each of the 5 image fields with an image over 2mb and wonder why the page timed-out or is blank after a couple of minutes.

Can PHP or something check this before the form gets rolling, then warn them about the size or time it will take to load these images?

eelixduppy

9:23 pm on Jan 11, 2007 (gmt 0)



When they submit the form, sum up the values of all $_FILES['userfile']['size'] and compare them to your limit in bytes. If it exceeds the limit, produce an error message and handle accordingly.

Good luck! :)

phranque

1:09 pm on Jan 12, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



typically you won't have this information available "in time" to help the surfer/uploader because the script won't be invoked by the server until the POST is completed, which means the files have already been uploaded to the server.

henry0

1:27 pm on Jan 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Eelix is correct
you need to check for file size
and for type of file allowed
for ex: Jpeg, jpg, gif etc.. but disallow JS etc.

cmarshall

4:04 pm on Jan 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can PHP or something check this before the form gets rolling, then warn them about the size or time it will take to load these images?

PHP, being a server-side language, cannot do anything until the file has been sent (closing the barn doors after the horses have run away).

JavaScript does not allow any interaction with the file system (Thank God!), so it can't help you.

eelixduppy

9:04 pm on Jan 12, 2007 (gmt 0)



The file is uploaded, yes, but to a temporary directory until you invoke the move_uploaded_file [us2.php.net] function. Going back to my first post on this matter, you can check the file size using something similar to this:

$size = $_FILES['userfile']['size'];
if($size > 30000) {
echo 'File size too large!';
exit();
}

And Henry brings up a good point also. Make sure that you are receiving the type of file that you want to be receiving by checking

$_FILES['userfile']['type']
for the appropriate mime types.

:)

supermanjnk

9:22 pm on Jan 12, 2007 (gmt 0)

10+ Year Member



I found a script at one time that used a combination of JS and iframes to upload the image without reloading the page.

Try searching for this in google
"php asynchronous image upload"
(without the quotes)

You could modify this to just do a check and if it's to big, return a message of some sort, or if it's big enough, let it continue to upload to a temp directory, then when they submit, move the image to a new directory.

phranque

10:17 pm on Jan 12, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



temp directory or not, the timeout issue remains - there is no way to interrupt the upload until it is too late...

henry0

11:35 pm on Jan 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



phranque is correct.

you should review this thread

Coopster explains
[webmasterworld.com] file upload process
that thread has many good info.

phranque

11:46 pm on Jan 12, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



so according to the "Coopster explains" thread, a progress bar is possible in c, php and maybe perl.
it seems to me if you can do a progress bar, you can interrupt the progress...

eelixduppy

12:00 am on Jan 13, 2007 (gmt 0)



>>>before uploading begins

It seems that the subtitle escaped me :)

barns101

1:25 pm on Jan 14, 2007 (gmt 0)

10+ Year Member



Have a look at the <input type="hidden" name="MAX_FILE_SIZE" value="x" /> element in the PHP: Handling file uploads [php.net] documentation.

It is supposed to be an advisory to the browser not to upload a larger file than specified, but in my experience it doesn't work. Maybe you'll have better luck? ;)

eelixduppy

1:58 pm on Jan 14, 2007 (gmt 0)



barns101, nice one :)

It should work correctly, but on your action page you would want to put something like this:


if($_FILES['userfile']['error'] === 0) {
//proceed with upload
} else {
//an error has occurred
}

Error Messages Explained [us2.php.net].

I learn something every day. ;)

P.S. Don't forget that the hidden input stated above must come before the file input.

barns101

3:52 pm on Jan 14, 2007 (gmt 0)

10+ Year Member



I learn something every day.

Me too! I never knew that you had to check for error within the action script. I though that the browser was supposed to alert the user or something.

henry0

11:05 pm on Jan 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Even better; grab it when it's all said :)
thanks both!