Tuesday, February 3, 2009

File uploading issue

Recently, I was writing a php script for uploading files.Strangely enough, I was able to upload all common types of files i.e.*.mpeg,*.avi,*.swf files but i was unable to upload flash files.

As i was aware that the default file upload size,controlled by upload_max_filesize php.ini directive is 2MB and the default maximum size of post Data that PHP accepts, controlled by post_max_size php.ini directive is 8MB, i tried changing value of upload_max_filesize using ini_set() function to 8MB. But there was no effect of this as i found that ini_set() doesn't necessarily change all the directives.

Then as a test, i made following changes directly in php.ini file:
1. set upload_max_filesize to 8MB
2. set post_max_size to 9MB
And voila, it worked. I was able to upload flash files(.flv) as well alongwith other video file types.

Note: Files are usually POSTed to the webserver using 'multipart/form-data' encoding format.What post_max_size does is,to set the upper limit on the amount of data that a script can accept.

Hence,post_max_size is the upload_max_filesize plus the sum of the lengths of all the other fields in the form.

Finally, after checking php manual and php.net link, i figured out following rules to follow before using ini_set() function

1. Make sure that the "CHANGEABLE" attribute of the php.ini directive whose value you would like to alter, is either PHP_INI_USER OR PHP_INI_ALL. Only those directives with these "CHANGEABLE" values can be modified with ini_set() function.

2. In the above case, make changes in php.ini as mentioned above.

Creative Commons License