PHP File Upload progress tracking PHP5.2 has added this feature

Source: Internet
Author: User
Tags apc php file upload

PHP V5.2 adds a hook for developers to track File Upload progress in real time. This article is part 1 of the "new features in PHP V5.2" series (five in total). It will show you how to monitor file uploads, write code, and create a PHP progress bar.

Web 2.0 is the hottest buzzword on the Internet, and investors have invested money in investment projects involving this technology. There are many descriptive terms covered by millions of Web sites and applications. Using Web 2.0, we will describe a type of Web sites that provide a way to understand the voice of millions of users on the Internet. The difference is that they all provide users with a place to exchange and share views and data related to common interests. These sites can quickly generate a large amount of content.

Every user will provide something-Comment on coffee shops, work routes, etc. YouTube is an excellent example in this regard, providing a space for people to upload videos, so that other users can watch these videos and provide feedback. YouTube is a new favorite of Web 2.0 practitioners. It is worth noting that so far YouTube's popularity has increased faster than any other site on the Internet. This popularity can be attributed to a large variety of content, and allows users to express their views on the content in the form of messages. In addition, you can not only leave a message, but also upload a video message corresponding to the video.

Text Field

Many Web sites that receive files place a nasty Browse button next to the text box, forcing users to upload a file at a time. This may take a long time, especially when videos, even photos, or other projects are provided in the form of small file groups. Each file must be uploaded separately, which may be cumbersome. Assuming that the time spent uploading super large files will make users with insufficient patience feel intolerable, it is important to provide positive feedback to these users to prevent them from giving up and leaving.

Fortunately, the new hook introduced by PHP V5.2 to the file upload process allows us to display the upload progress to users in real time. In this article, we will use PHP V5.2 to create a progress bar for the user (to obtain the source code, click to download ).

Complete description

If the correct library is installed and configured, the new "hook" in PHP V5.2 is actually a data point that can be obtained during file transfer. These new hooks use a function called Alternative PHP Cache. When the PHP script receives an uploaded file, the interpreter will automatically check $ _ POST array to find the hidden field named APC_UPLOAD_PROGRESS, which will become a cache variable, store the uploaded information so that the script can access the uploaded file. When this information is cached and accessible at any time, you can provide visual feedback to users to improve user experience.

We will introduce the implementation of APC code in HTML forms, and how to identify the implementation in PHP and how to access the cached information. There are many methods for this data: from Ajax to FLEX, but we need to pay attention to the methods required to access data for preparing these front-end technologies.

Set

The APC in PHP V5.2 is not enabled by default. Since the new hook is part of APC, make sure to install the extension and make it available for the PHP interpreter. This will be done by downloading the php_apc extension file. In our example, WAMP will be used for installation, which includes free packages of Apache and MySQL for PHP for Windows.®. It provides a friendly user interface and is easy to manage with menus that support configuration options.

To set APC on WAMP, perform the following steps:

To download the library and WAMP, see references.

Install WAMP.

Put the php_apc.dll file in the PHP extension folder. By default, this folder is /Php/ext.

Use the WAMP menu of the system disk to choose PHP settings> PHP Extensions> Add Extension.

In the displayed command line interface, type php_apc.dll and press Enter.

Use a text editor to open /Php. ini and add the code line apc. rfc1867 = on (add to any location ). If you want to test locally and upload large files to see the progress, add the following command: apc. max_file_size = 200 M, upload_max_filesize = 200 M, and post_max_size = 200 M. Do not perform this operation on the active generation server. However, otherwise, it is likely that the bandwidth and disk space quota will be exhausted, not to mention reducing the access speed of others.

Restart PHP.

APC should be set and initialized now. The RFC1867 feature of APC-enables you to track File Upload features-should now be enabled as an option and be prepared to explore File Upload to enable real-time status.

Accounts that can receive files

To receive files, you must first set the form for receiving files. It is very convenient that HTML comes with the standard field type of the file. Like all HTML form fields, it is logically named as type file. By default, the convenient Browse button displayed on the right of the block is attached.

 

Listing 1. HTML form of upload. php

 

 
 
  1. Reference content is as follows:
  2. <? Php
  3. $ Id = $ _ GET [id];
  4. ?>
  5.  
  6. <Form enctype = "multipart/form-data" id = "upload_form"
  7. Action = "target. php" method = "POST">
  8.  
  9. <Input type = "hidden" name = "APC_UPLOAD_PROGRESS"
  10. Id = "progress_key" value = "<? Php echo $ id?> "/>
  11.  
  12. <Input type = "file" id = "test_file" name = "test_file"/> <br/>
  13.  
  14. <Input onclick = "window. parent. startProgress (); return true ;"
  15. Phperz.com
  16.  
  17.  
  18. Type = "submit" value = "Upload! "/>
  19.  
  20. </Form>

You need to create a PHP page for this form because you need to use a unique key to track uploads. Finally, it will be used to call this page as part of the GET value URL. This number will be the value of the APC cache entry key to be retrieved later. To pass this value, the form field must have a hidden field with a special name, so that APC knows that it needs to save the file upload status. This field is called APC_UPLOAD_PROGRESS. This is the hook for starting the cache process. To ensure that PHP can access the correct entries in the cache, we use the retrieved unique ID as the value of the hidden field to create the key for this value. After the user submits the form-we will briefly process the submit button-the browser will send the file and key as part of the POST data sent to the server.

 

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.