I built a website which allow people to upload their photos using php, but the phots can't be successfully uploaded if the image size is too big, how can I avoid that problem, how can I make the people upload the image without the size limitation, could you show me some php code relevant to that if possible, thanks!
1 Answer
This is possible with .htaccess. You'll need to add the following lines to your file:
<IfModule mod_php5.c> php_value upload_max_filesize 20M </IfModule> Although it's probably better to increase the script execution time (so it doesn't time out waiting for the upload) and the maximum POST size, thus:
<IfModule mod_php5.c> php_value upload_max_filesize 20M php_value post_max_size 20M php_value max_execution_time 120 </IfModule> This sets the maximum execution time to two minutes, which is a very long amount of time, however this is just an example.
PHP solution (won't work - see comments)
First, turn on error checking; this will let you know what's happened and where by putting this at the top of your script:
error_reporting(E_ALL); ini_set('display_errors', '1'); You'll need to change the upload_max_filesize and the post_max_size declarations in your php.ini (assuming you're using Apache).
Find a lines that look similar to this:
upload_max_filesize = 2M post_max_size = 8M And change it to something like:
upload_max_filesize = 20M post_max_size = 20M This sets the upload limit to 20MB (which is rather large, I must say). If you're on shared hosting, it's very unlikely they'll allow you to edit php.ini. In this case, you can use ini_set() like such:
string ini_set('upload_max_filesize', '20M'); string ini_set('post_max_size', '20M'); This will set the upload limit to 20MB, or whatever you want to set it to.
(Thank you to @Phil in the comments) Do note that upload_max_filesize must be less than or equal to post_max_size.
9 Comments
post_max_size which must be equal to or larger than upload_max_filesize