Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MAX_FILE_SIZE, stop upload of files that are too big

Status
Not open for further replies.

EvilErnie

Programmer
Feb 25, 2002
2
DE
Hi there...

Goodness, this could be a Newbie question. I read the question quite a few times, but i never found a good answer. Maybe you somebody here can help.

I got a gallery set up with mySQL and PHP4. I want the users to be able to upload images, but of course i want to limit the filesize. That is no problem AFTER the request is sent - i just read $file_size and print an error message.

But if somebody uploads an image that is - say - 4 MB it gets loaded to the temp folder on the server, THEN i check the file size and exit the process.

When i set MAX_FILE_SIZE to a small value, there will be a quite ugly error message, if the file is bigger.

So, is there a possibility to check the file size, BEFORE the file is sent to the server, or at least catch that error message?

Regards,
EvilErnie
 
To the question you made, i don't know the answer, but i can tell you that you can change the paramters of the php.ini file in runtime, with the function ini_set

ini_set("MAX_FILE_SIZE","10M");

changes the MAX_FILE_SIZE of this script to 10 MB.
Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Here is a trick that can help, but it is most definitely not bulletproof. As Anikin suggested, set the proper parameter in your php.ini file.

Create a form field as such:
<input type=”hidden” name=MAX_FILE_SIZE” value=”1000”>

value is the maximum size in bytes that you want to allow.

Place this field before your <input type=”file”> tag.

I hope that helps.
 
Hi there...

Thanx for the help, but that is not it.

Writing <input type=”hidden” name=MAX_FILE_SIZE” value=”1000”> in front of the input file...-field CREATES the error message i want to get rid of.

The only way right now is to set the MAX_FILE_SIZE to lets say 10 MB, wait for an upload and THEN say on the target page &quot;Well, that file is too big!&quot;. But then the file has already been uploaded to the temp folder on the server - and created traffic.

I am looking for a method, that skips the upload and determines the filesize BEFORE the request is sent.

Can i check the file size of a file on the local computer via PHP? That way, i could take the path, the user enters, have a look at the file size on HIS computer and tell him, whether he is allowed to upload or not.

Any idea,
EvilErnie
 
That way you have to do some Scripting in JS and not in PHP. That's for sure. I would like to help you, but i can't. Sorry.
Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top