tags:

views:

82

answers:

5

I was wondering how would you limit an uploaded image size to 5mb using PHP & MySQL.

+4  A: 

Check out the upload_max_filesize ini directive.

Andy E
+2  A: 

depends on the method of upload really. For example within a standard HTML form you could add something like:

<input type="hidden" name="MAX_FILE_SIZE" value="50000" />

There are also options from within PHP itself that can usually be controlled through .htaccess files, like this:

php_value upload_max_filesize 5M
php_value post_max_size 5M

or through ini_set() - all depends on your setup really.

seengee
this could be bypassed using a remote form on a different site.
Andy E
Couldn't a user with firebug or something just change the "value" attribute on your hidden field and submit with whatever limit they wanted?
Austin Fitzpatrick
yep, there are always security considerations with HTML such as this. All depends on context though really.
seengee
Just to clarify, the HTML option is inherently an insecure solution, but under certain circumstances it can still be suitable.
seengee
+1  A: 

Use if ($_FILE['yourfile']['size'] > (1024 * 5)) or restrict php using ini_set('upload_max_filesize', '5MB');

Danten
Can you use ini_set for upload_max_filesize? I thought that the file had already been "passed" by the time the PHP code runs?
Andy E
A: 

Check out this link for all the details, but it will depend on the MAX_FILE_SIZE for the form, the upload_max_filesize and post_max_size in php.ini. Some other parameters may come into play as well (e.g. max_input_time or memory_limit if the max file size is large enough).

Eric Petroelje
+1  A: 

There are a couple of ways. The quickest, but least effective is to set the MAX_FILE_SIZE size in the HTML form.

The most effective is to change the PHP ini. You need to set the following variables:

dnagirl