PHP File upload Progress Tracking PHP5.2 has added this feature _php tutorial

Source: Internet
Author: User
Tags apc mysql free php file upload
PHP V5.2 adds hooks to developers to take advantage of the ability to track file uploads in real time. This article is part 5th of the "What's new in PHP V5.2" series (a total of five parts) and will show you how to monitor file uploads and write code accordingly, and how to create a PHP progress bar.

WEB 2.0 is the hottest buzzword on the Internet, with investors putting money into investment projects that deal with the technology. There are a number of descriptive terms covered by millions of Web sites and applications. Using Web 2.0, we describe a class 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 a place for users to communicate and share ideas and data that are relevant to their common interests, which can generate a lot of content quickly.

Each user will be provided with some sort of content-commenting on the coffee shop, the route to work, etc. YouTube is an excellent example of this, providing people with a space to upload videos and enable other users to view and provide feedback on these videos. YouTube is a favorite of Web 2.0 followers, and it's worth noting that YouTube's popularity has risen so far faster than any other site on the Internet. This popularity can be attributed to a wide variety of content, as well as the ability to let users express their views on content in the form of messages. And not only can you leave a message, users can even upload video messages that correspond to the video.

Text field

Many Web sites that receive files will place an offensive Browse button next to the text box, forcing users to upload one file at a time. This can take a long time, especially if you are providing video, even photos, or other items in the form of a small filegroup. Because each file must be uploaded separately, it can be cumbersome. Assuming that the time it takes to upload a very large file can be intolerable to impatient users, it is important to give these users positive feedback to prevent them from giving up and going away.

Fortunately, the new hooks that PHP V5.2 introduced into the file upload process allow us to show the user the progress of the upload in real time. In this article, you will use PHP V5.2 to create a progress bar for the user (to get the source code, click Download).

Full 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 will use a feature called alternative PHP Cache. When the PHP script receives an upload file, the interpreter automatically checks the $_post array for a hidden field named Apc_upload_progress, which becomes a cache variable that stores information about the upload so that the script can access the uploaded file. When this information is cached and readily accessible, users can be provided with visual feedback to improve the user experience.

We will describe the implementation of the APC code in the HTML form, and how to identify the implementation in PHP and how to access the cached information. There are a number of ways to represent this data: from Ajax to FLEX, but what we want to focus on is how to prepare the access data needed for these front-end technologies.

Set up

By default, APC in PHP V5.2 is not enabled. Since the new hook is part of the APC, it is necessary to ensure that the extension is installed and made available to the PHP interpreter. This will be done by downloading the PHP_APC extension file. In our example, we will use the WAMP installation, which is included in Apache and MySQL free to get packaged PHP for Windows®. It provides a friendly user interface and is easy to manage thanks to a menu that supports configuration options.

To set up APC on WAMP, perform the following steps:

To download libraries and WAMP, see Resources.

Install WAMP.

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

Use the system tray WAMP menu to select PHP settings>php extensions>add Extension.

In the command line interface that appears, type Php_apc.dll and press Enter.

Using a text editor, open /php/php.ini and add code line apc.rfc1867 = On (add to any location). If you are trying to test locally and plan to upload large files so that you can actually see the progress, you will also need to add the following directive: Apc.max_file_size = 200M, upload_max_filesize = 200M, and post_max_size = 200M. Do not perform this operation on the active build server, however, it is likely that you will run out of bandwidth and disk space quotas, not to mention slowing down other people's access.

Restart PHP.

APC should now be set up and initialized. APC's RFC1867 feature--allows you to track file upload features--should now be enabled as an option, and should be ready to explore file uploads to enable real-time status.

Accounts that can receive files

To receive a file, you must first set up a form to receive the file. Conveniently, HTML comes with the standard field type of the file. As with all HTML form fields, it is logically named type file. By default, the handy Browse button that appears to the right of the block is included.

Listing 1. HTML form for upload.php

 
 
  1. The following is the referenced content:
  2. PHP
  3. $ ID = $_get[id];
  4. ?>
  5. < form enctype="multipart/form-data" id=" Upload_form "
  6. Action = "target.php" method="POST">
  7. < input type="hidden" name="Apc_upload_progress"
  8. ID = "Progress_key" value=" " />
  9. < input type="file" id="test_file" name = "Test_file" /> < BR />
  10. < input onclick="window.parent.startProgress (); return true; "
  11. Phperz.com
  12. type = "Submit" value="upload!" />
  13. form >

You need to create a PHP page for this form because you need to use the unique key to track the upload. Finally, it will be part of the URL used to call this page as a GET value. This number will be the value of the APC cache entry key that will be retrieved later. To pass this value, the form field needs to have a hidden fields with a special name so that APC knows it needs to save the file upload status. This field is called Apc_upload_progress. This is the hook for the boot caching process described above. To make sure 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 that value. After the user submits the form-we will briefly process the submit button-the browser will send the file and the key as part of the post data sent to the server.

 

http://www.bkjia.com/PHPjc/486115.html www.bkjia.com true http://www.bkjia.com/PHPjc/486115.html techarticle PHP V5.2 adds hooks to developers to take advantage of the ability to track file uploads in real time. This article is the 5th part of the new feature series in PHP V5.2 (a total of five parts) that will show you ...

  • Related Article

    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.