Php file Upload progress bar implementation program. There are many methods to implement the upload progress bar in php. for example, ajax is currently the mainstream or iframe is used for implementation, now we will introduce php's apc and uploadprogress to implement the file upload progress bar effect. There are many ways to implement the upload progress bar in php, such as ajax is currently the mainstream or using iframe to implement it, now we will introduce the php apc and uploadprogress implementation of the file upload progress bar effect.
Currently, I know two methods: one is to use the APC extension module written by Rasmus Lerdorf, founder of PHP to implement (http://pecl.php.net/package/apc ), another way is to use the PECL extension module uploadprogress implementation (http://pecl.php.net/package/uploadprogress) I here are two examples of implementation respectively for reference, more flexible application according to their own needs to modify.
APC implementation method:
Install APC by referring to the official documentation. you can use the PECL module to install APC quickly and easily.
Configure php. ini and set the parameter apc. rfc1867 = 1 to enable APC to support the upload progress bar function, which is described in the APC source code instructions.
Sample code:
The code is as follows: |
|
If ($ _ SERVER ['request _ method'] = 'post') {// upload REQUEST $ Status = apc_fetch ('upload _ '. $ _ POST ['apc _ UPLOAD_PROGRESS']); $ Status ['Done'] = 1; Echo json_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 ['SS SS _ key']); Echo json_encode ($ status ); Exit; } Else { // Other code, such as uploading a form } |
Implementation of the uploadprogress module:
Use the PECL module installation method to install this module
Set uploadprogress. file. filename_template = "/tmp/upd_%s.txt" in php. ini"
Sample code:
The code is as follows: |
|
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, 26 Jul 1997 05:00:00 GMT "); Header ("Last-Modified:". gmdate ("D, d m y h: 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 )){ Echo json_encode ($ uploadvalues ); } Else { // Read progress failed. additional processing logic } } Else { // Display the upload form } |
Integration
The rest is to hook all the content together. You can perform this operation on the progress. php page.
Listing 5. final progress. php page
The code is as follows: |
|
$ Id = uniqid (""); ?>
Upload Example
Type = "text/javascript"> script |