How to Implement the PHP File Upload progress bar

Source: Internet
Author: User

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:

 
 
  1. If ($ _ SERVER ['request _ method'] = 'post') {// upload REQUEST
  2. $Status=Apc_fetch('Upload _ '. $ _ POST ['apc _ UPLOAD_PROGRESS']);
  3. $ Status ['done'] = 1;
  4. Echo json_encode ($ status); // ajax call output to the user's page. Please find the relevant documentation
  5. Exit;
  6. } Elseif (isset ($ _ GET ['ss SS _ key']) {// read the upload progress
  7. $Status=Apc_fetch('Upload _ '. $ _ GET ['Progress _ key']);
  8. Echo json_encode ($ status );
  9. Exit;
  10. } Else {
  11. // Other code, such as uploading a form
  12. }

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:

 
 
  1. If ($ _ SERVER ['request _ method'] = 'post '){
  2. If (is_uploaded_file ($ _ FILES ['upfile'] ['tmp _ name']) {
  3. $Upload_dir='Your _ path /';
  4. $Ext=Strrchr($ _ FILES ['video'] ['name'], '.');
  5. $Sessid= $ _ POST ['upload _ identifier'];
  6. $Tmpfile= $ Upload_dir. $ sessid;
  7. $Sessfile= $ Upload_dir. $ sessid. $ ext;
  8. If (move_uploaded_file ($ _ FILES ['upfile'] ['tmp _ name'], $ tmpfile )){
  9. // Upload successful
  10. } Else {
  11. // Upload Failed
  12. } Else {
  13. // Upload Error
  14. } Elseif (! Empty ($ _ GET ['sessid ']) {
  15. Header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT ");
  16. Header ("Last-Modified:". gmdate ("D, d m y h: I: s"). "GMT ");
  17. Header ("Cache-Control: no-store, no-cache, must-revalidate ");
  18. Header ("Cache-Control:Post-check=0,Pre-check=0", False );
  19. Header ("Pragma: no-cache ");
  20. Header ("Content-Type: text/html;Charset=UTF-8 ");
  21.  
  22. $Unique_id= $ _ GET ['sessid '];
  23. $Uploadvalues=Uploadprogress_get_info($ Unique_id );
  24.  
  25. If (is_array ($ uploadvalues )){
  26. Echo json_encode ($ uploadvalues );
  27. } Else {
  28. // Read progress failed. Additional processing logic
  29. }
  30. } Else {
  31. // Display the upload form
  32. }

The above are the two methods for uploading progress bars of PHP files.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.