Novice must see PHP upload file Progress full disclosure _php tutorial

Source: Internet
Author: User
Tags apc configuration php
PHP is still more commonly used, so I studied the PHP upload file progress, here to take out and share with you, hope to be useful to everyone. PHP is very difficult to implement the upload progress bar is because when we upload files to the server, we have to wait until all the files are sent to the server, the corresponding PHP files are executed. Before that, the file data is stored in a temporary file, and PHP cannot get the path and size of the file.

Starting with ActionScript 2.0, Flash supports file uploads and downloads. Although it is not possible to get the file upload progress on the server, we can get the progress of sending the file on the server. According to this principle, flash can be used to make upload progress bar effect. I have seen some information on the internet, but I feel defective. So I studied it, on the basis of the predecessors to enhance the security and robustness of the program, and added some customizable parameters. There are two ways I know now, one is to use the APC extension module written by the founder of PHP Rasmuslerdorf, and the other is to use the PECL Extension Module uploadprogress to implement the example I have here to give two separate implementations for reference. More flexible applications are modified to suit their needs.

the APC implementation method for uploading file progress in PHP :

Install APC, refer to the Official document installation, you can use the Pecl module installation method quick and simple, here does not explain the configuration php.ini, set the parameters apc.rfc1867=1, so that APC support the upload progress bar function, in the APC source code documentation, there is a description of the codes example:

 
 
    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);//output to Ajax calls in user-side pages, related documents please find yourself
    5. exit;
    6. }elseif (isset ($_get[' Progress_key ')) {//Read upload progress
    7. $ status = apc_fetch (' Upload_ '. $_get[' Progress_key ');
    8. Echojson_encode ($status);
    9. exit;
    10. }else{
    11. //other code, such as upload form etc.
    12. }

PHP Upload file Progress of the Uploadprogress module implementation method :

Install the module using the PECL module installation method php.ini uploadprogress.file.filename_template= "/tmp/upd_%s.txt" code example:

 
 
  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,26jul199705:00:00gmt");
  16. Header ("last-modified:". Gmdate ("D,dmyh: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. $ unique_id =$_get[' sessid '];
  22. $ uploadvalues = Uploadprogress_get_info ($unique _id);
  23. if (Is_array ($uploadvalues)) {
  24. Echojson_encode ($uploadvalues);
  25. }else{
  26. Read progress failed, additional processing logic
  27. }
  28. }else{
  29. Show Upload Form
  30. }

http://www.bkjia.com/PHPjc/446544.html www.bkjia.com true http://www.bkjia.com/PHPjc/446544.html techarticle PHP is still more commonly used, so I studied the PHP upload file progress, here to take out and share with you, hope to be useful to everyone. PHP is very difficult to implement the upload progress bar is ...

  • 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.