Tuesday, May 20, 2008

Increase file upload size in PHP

In web technology file is most vital factors. Files like image are using last decade. Now time comes for audio and video. Peoples are not happy with only images. More exciting with audio and video what are more realistic presenter for any web site. As a developer i found difficulties while upload audio and video files. Image, audio and video files are same. I mean the upload process. All treat just as files. For images i don't get any problem because images are small in size normally. About audio and video these file formats are number of times greater in size then images. So i fall in problem.

There are two considerable factors for upload any file. These are
1. Execution time
3. File Size.

Your script must need to execute until the file is uploaded completely. And offcourse your server alow your expected upload file size. Php have default configuration for both issue. Execution time is set by default 60 seconds. And allowed maximum file size is up to 2MB. Now what shall you do for a audio or video file. Your internet connection might slow and need more execution time then default. Again you may need more file size to be allowed then 2MB. The question is how??

To increase your execution time write a single line of code at the beginning of your script. here it is:
set_time_limit(0);
?>

Now the big issue. Have to increase maximum file size as you need. One solution like:
ini_set("upload_max_filesize",30);
?>
For view current maximum allowed size (before and after change) use echo like:
echo ini_get("upload_max_filesize");
?>

Now the most critical one. Though almost all server allow set_time_limit(0) but Most of the server doesn't allow ini_set("upload_max_filesize",30). I find one more alternatives. You can edit the maximum size by using .htaccess. Means create a file named .htaccess write the following code in that file. Now upload it at the same path where your source file is. Now run your script and test with echo ini_get("upload_max_filesize"); 

.htaccess file code:

php_value upload_max_filesize "25M"

Enjoy your coding....... If you get any more solution please let me know. I am most novice one so i don't know much but like to learn.

No comments: