New users must thoroughly reveal the progress of PHP File Upload

Source: Internet
Author: User

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:

 
  1. If ($ _ SERVER ['request _ method'] = 'post') {// upload REQUEST
  2. $Status=Apc_fetch('Upload _ '. $ _ POST ['apc _ UPLOAD_PROGRESS']);
  3. $ Status ['done'] = 1;
  4. Echojson_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. Echojson_encode ($ status );
  9. Exit;
  10. } Else {
  11. // Other code, such as uploading a form
  12. }

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:

 
  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.  
  15. } Elseif (! Empty ($ _ GET ['sessid ']) {
  16. Header ("Expires: Mon, 26Jul199705: 00: 00GMT ");
  17. Header ("Last-Modified:". gmdate ("D, dMYH: I: s"). "GMT ");
  18. Header ("Cache-Control: no-store, no-cache, must-revalidate ");
  19. Header ("Cache-Control: post-check=0,Pre-check=0", False );
  20. Header ("Pragma: no-cache ");
  21. Header ("Content-Type: text/html;Charset=UTF-8 ");
  22.  
  23. $Unique_id= $ _ GET ['sessid '];
  24. $Uploadvalues=Uploadprogress_get_info($ Unique_id );
  25.  
  26. If (is_array ($ uploadvalues )){
  27. Echojson_encode ($ uploadvalues );
  28. } Else {
  29. // Read progress failed. Additional processing logic
  30. }
  31.  
  32. } Else {
  33. // Display the upload form
  34. }

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.