In this article I am going to write my experience with a work in which I was supposed to do file upload progress bar.
First of all I consider using APC then came to know about PECL extension uploadprogress
Unfortunately both were not possible for me as I have no control on the server on which it was supposed to run.
Tweaking with installed version of PHP or up gradation of PHP was also ruled out.
In India we have a word which is very common in use whenever someone is in a deadlock, the magic word is ‘Jugad’, if simply translated into English, it will mean ‘system’, but in contextual meaning it stands for ‘Smart Work’.
Well I can not claim that adjective for this work, as it has some contextual limitations but it is working solution.
Before I jump to the final details of the working structure, I would like to recollect some facts about file uploading in PHP.
Whenever you upload something via a PHP page, it gets uploaded to a temporary folder on server then programmer has to move it from there to a proper location via PHP script.
That temporary folder can be defined in php.ini file and if not defined PHP uses operating systems default temporary folder.
I have created this script with the help of Ajax.
First of all I have changed temporary folder of PHP to a folder on which I have some control so I can access that with my PHP scripts time to time. Then I have created a form to upload the file to server.
I created another PHP script to calculate the size of a particular folder and THATS’S IT application was finished.
Whenever someone hits the upload button, file upload starts to the server.Using Ajax I call my other script which that calculates the size of temp folder after every second, and then I show the updated html page with file size using Ajax.
You can call it dirty programming but it is working solution to this problem in any version of PHP.
When this solution will not work?
It will always work but in some situations may give you wrong results. This approach will not produce desired results on the servers where lots of file handling is being done via PHP and lots of people are doing file upload at one go.
Although it can be designed to have multiple file upload at same time like instead of temporary folder we can opt for calculation of particular temporary file in temporary folder and that file can be guessed by the upload button hit time, but that is not 100% usable in every situation. If lots of people are uploading the files at same time it may conflicts with each other.
Did you like this Jugad?