PHP is still quite common, So I studied the PHP File Upload progress and shared it with you here. I hope it will be useful to you. PHP is difficult to implement the upload progress bar because when we upload files to the server, we need to wait until all the files are sent to the server before executing the corresponding PHP file. Before that, the file data is stored in a temporary file, and php cannot obtain the path and size of the file.
Flash supports uploading and downloading files starting with Actionscript 2.0. Although the File Upload progress cannot be obtained on the server side, we can get the file sending progress on the server side. Based on this principle, you can use Flash to make the upload progress bar effect. I have seen some materials on the Internet, but I feel defective. So I studied it myself and strengthened the security and robustness of the program based on the predecessors, and added some customizable parameters. Currently, I know two methods. One is to use the APC extension module written by RasmusLerdorf, founder of PHP, another method is to use the PECL extension module uploadprogress for implementation. Here are two implementation examples for reference. More flexible applications can be modified as needed.
PHP File Upload progress: APC implementation:
Install APC by referring to the official documentation. You can use the PECL module to install APC quickly and easily. ini, set the apc parameter. rfc1867 = 1 to enable APC to support the upload progress bar function. The following code examples are provided in the APC source code instructions:
- If ($ _ SERVER ['request _ method'] = 'post') {// upload REQUEST
- $Status=Apc_fetch('Upload _ '. $ _ POST ['apc _ UPLOAD_PROGRESS']);
- $ Status ['done'] = 1;
- Echojson_encode ($ status); // ajax call output to the user's page. Please find the relevant documentation.
- Exit;
- } Elseif (isset ($ _ GET ['ss SS _ key']) {// read the upload progress
- $Status=Apc_fetch('Upload _ '. $ _ GET ['Progress _ key']);
- Echojson_encode ($ status );
- Exit;
- } Else {
- // Other code, such as uploading a form
- }
PHP File Upload progress: uploadprogress module implementation:
Use the PECL module Installation Method to install uploadprogress. file. filename_template = "/tmp/upd_%s.txt" in the php. ini module. Example code:
- If ($ _ SERVER ['request _ method'] = 'post '){
- If (is_uploaded_file ($ _ FILES ['upfile'] ['tmp _ name']) {
- $Upload_dir='Your _ path /';
- $Ext=Strrchr($ _ FILES ['video'] ['name'], '.');
- $Sessid= $ _ POST ['upload _ identifier'];
- $Tmpfile= $ Upload_dir. $ sessid;
- $Sessfile= $ Upload_dir. $ sessid. $ ext;
- If (move_uploaded_file ($ _ FILES ['upfile'] ['tmp _ name'], $ tmpfile )){
- // Upload successful
- } Else {
- // Upload Failed
- } Else {
- // Upload Error
-
- } Elseif (! Empty ($ _ GET ['sessid ']) {
- Header ("Expires: Mon, 26Jul199705: 00: 00GMT ");
- Header ("Last-Modified:". gmdate ("D, dMYH: I: s"). "GMT ");
- Header ("Cache-Control: no-store, no-cache, must-revalidate ");
- Header ("Cache-Control: post-check=0,Pre-check=0", False );
- Header ("Pragma: no-cache ");
- Header ("Content-Type: text/html;Charset=UTF-8 ");
-
- $Unique_id= $ _ GET ['sessid '];
- $Uploadvalues=Uploadprogress_get_info($ Unique_id );
-
- If (is_array ($ uploadvalues )){
- Echojson_encode ($ uploadvalues );
- } Else {
- // Read progress failed. Additional processing logic
- }
-
- } Else {
- // Display the upload form
- }