When writing file upload code, we also need to consider an important implementation function, that is, the progress bar required for file upload. Now I knowHere are two implementation examples for reference. More flexible applications can be modified as needed.
Implementation of the PHP File Upload progress bar of APC:
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:
- 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 ['Progress _ key']);
- Echo json_encode ($ status );
- Exit;
- } Else {
- // Other code, such as uploading a form
- }
Implementation of the uploadprogress module:
How to Use the PECL module Installation Method to install the PHP File Upload progress bar of this module
Set uploadprogress. file. filename_template = "/tmp/upd_%s.txt" in php. ini"
Sample 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, 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
- }
The above are the two methods for uploading progress bars of PHP files.