0

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
  • 2
    php always gives you a hint what has happened wrong. Turn on errors logging (if it hasn't been done yet) and read it. Commented Aug 29, 2011 at 0:33

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

9 Comments

Don't forget post_max_size which must be equal to or larger than upload_max_filesize
where should I put this in my php code, I tried to upload a 4MB picture and used the above code, but still not working, why?
@Jam: you cannot change the ini variables from within the PHP script that handles the upload - by the time the script is started, PHP's already aborted the upload and the ini_set will never get a chance to work. it HAS to be done at the .ini (or .htaccess) level.
I need an simple example, can you show me how it exactly works in ini or hataccess level, what I need to do, thanks!
@Marc Could you not set them in the script that outputs the upload form? Or are the values reset as soon as PHP is done executing?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.