PHPV5.2 adds a hook for developers to track file Upload progress in real time. This article is part 1 of the new feature series (five in total) in PHPV5.2. it will show you how to monitor file uploads and write code accordingly, and how to create a PHP progress bar. Web2.0 is the most popular word on the Internet, "> <LINKhref
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
- Reference content is as follows:
-
- $ Id = $ _ GET ['id'];
- ?>
-
-
- Action = "target. php" method = "POST">
-
-
- Id = "progress_key" value =" "/>
-
-
-
-
- Phperz.com
-
-
- Type = "submit" value = "Upload! "/>
-
-
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.