Home > Backend Development > PHP Tutorial > PHP file upload progress tracking PHP5.2 has added this feature_PHP tutorial

PHP file upload progress tracking PHP5.2 has added this feature_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-13 17:41:49
Original
874 people have browsed it

PHP V5.2 adds hooks for developers to take advantage of the ability to track file upload progress in real time. This article, part 5 of a five-part series on "What's New in PHP V5.2," will show you how to monitor file uploads and code accordingly, as well as how to create a PHP progress bar.

Web 2.0 is the hottest buzzword on the Internet, and investors are pouring money into investment projects involving this technology. There are many descriptive terms covering millions of Web sites and applications. Using Web 2.0, we describe a class of Web sites that provide access to the voices of millions of users on the Internet. What sets them apart is that they all provide a place for users to communicate and share ideas and data related to common interests, and these sites can generate large amounts of content quickly.

Each user will provide some kind of content - reviews of coffee shops, routes to work, etc. YouTube is an excellent example of this, providing a space for people to upload videos and allow other users to view them and provide feedback. YouTube is the new favorite of Web 2.0 devotees, and it's worth noting that its popularity is rising faster than any other site on the Internet so far. This popularity can be attributed to the large variety of content and the ability for users to express their opinions on the content in the form of comments. And not only can you leave messages, users can even upload video messages corresponding to the video.

Text field

Many Web sites that accept files will place an annoying 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 provide videos or even photos or other items in small file groups. This can be cumbersome since each file must be uploaded individually. Assuming that the time it takes to upload very large files would be intolerable to impatient users, it would be important to provide these users with positive feedback to prevent them from giving up and walking away.

Fortunately, the new hook introduced in PHP V5.2 into the file upload process allows us to display the upload progress to the user 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 to download).

Full description

The new “hooks” in PHP V5.2 are actually data points available during file transfers if the correct libraries are installed and configured. These new hooks will use a feature called Alternative PHP Cache. When a PHP script receives an uploaded file, the interpreter will automatically check the $_POST array for a hidden field called APC_UPLOAD_PROGRESS, which will become 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, visual feedback can be provided to the user, thereby improving the user experience.

We will introduce the implementation of APC code in HTML forms, how to identify the implementation in PHP and how to access cached information. There are many ways to represent this data: from Ajax to FLEX, but what we want to focus on is preparing the methods that these front-end technologies need to access the data.

Settings

By default, APC is not enabled in PHP V5.2. Since the new hook is part of APC, you need to make sure 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 the free packaged PHP for Windows® that includes Apache and MySQL. It provides a user-friendly interface and is very easy to manage thanks to its menu that supports configuration options.

To set up APC on WAMP, follow these steps:

To download the library and WAMP, see Resources.

Install WAMP.

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

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

 In the pop-up command line interface, type php_apc.dll and press Enter.

Using a text editor, open /php/php.ini and add the line apc.rfc1867 = on (you can add it anywhere). If you are trying to test locally and plan to upload large files so you can actually see the progress, you will also need to add the following directives: apc.max_file_size = 200M, upload_max_filesize = 200M and post_max_size = 200M. Please do not do this on an active build server; however, failure to do so will likely exhaust your bandwidth and disk space quota, not to mention slowing down access for others.

Restart PHP.

APC should now be set up and initialized. The RFC1867 feature of APC - the feature that enables you to track file uploads - should now be enabled as an option, and you should be ready to explore file uploads to enable live status.

Accounts that can receive files

To receive files, you must first set up a form to receive files. Conveniently, HTML comes with standard field types for documents. Like all HTML form fields, it is logically named of type file. By default, it comes with a convenient Browse button that appears on the right side of the block.

Listing 1. HTML form of upload.php

<ol class="dp-xml">
<li class="alt"><span><span>以下为引用的内容:  </span></span></li>
<li>
<span class="tag"><?</span><span class="tag-name">php</span><span> </span></li><li class="alt"><span>   $</span><span class="attribute">id</span><span> = $_GET[id];  </span></li><li><span class="tag">?></span><span> </span>
</li>
<li class="alt"><span> </span></li>
<li>
<span class="tag"><</span><span class="tag-name">form</span><span> </span><span class="attribute">enctype</span><span>=</span><span class="attribute-value">"multipart/form-data"</span><span> </span><span class="attribute">id</span><span>=</span><span class="attribute-value">"upload_form"</span><span>   </span></li><li class="alt"><span>      </span><span class="attribute">action</span><span>=</span><span class="attribute-value">"target.php"</span><span> </span><span class="attribute">method</span><span>=</span><span class="attribute-value">"POST"</span><span class="tag">></span><span> </span>
</li>
<li><span> </span></li>
<li class="alt">
<span class="tag"><</span><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"hidden"</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"APC_UPLOAD_PROGRESS"</span><span>   </span></li><li><span>       </span><span class="attribute">id</span><span>=</span><span class="attribute-value">"progress_key"</span><span>  </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"<?php echo $id?>"</span><span class="tag">/></span><span> </span>
</li>
<li class="alt"><span> </span></li>
<li>
<span class="tag"><</span><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"file"</span><span> </span><span class="attribute">id</span><span>=</span><span class="attribute-value">"test_file"</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"test_file"</span><span class="tag">/></span><span class="tag"><</span><span class="tag-name">br</span><span class="tag">/></span><span> </span>
</li>
<li class="alt"><span> </span></li>
<li>
<span class="tag"><</span><span class="tag-name">input</span><span> </span><span class="attribute">onclick</span><span>=</span><span class="attribute-value">"window.parent.startProgress(); return true;"</span><span>   </span></li><li class="alt"><span>phperz.com  </span></li><li><span> </span></li><li class="alt"><span> </span></li><li><span class="attribute">type</span><span>=</span><span class="attribute-value">"submit"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"Upload!"</span><span class="tag">/></span><span> </span>
</li>
<li class="alt"><span> </span></li>
<li>
<span class="tag"></</span><span class="tag-name">form</span><span class="tag">></span><span> </span>
</li>
</ol>
Copy after login

A PHP page needs to be created for this form as a unique key is required 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 field with a special name that lets APC know that it needs to save the file upload status. This field is called APC_UPLOAD_PROGRESS. This is the aforementioned hook that starts the caching process. To ensure that PHP has access to the correct entry in the cache, we use the retrieved unique ID as the value of the hidden field, thereby creating a key for that value. After the user submits the form -- we'll deal with the submit button briefly -- the browser will send the file and key as part of the POST data to the server.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486115.htmlTechArticlePHP V5.2 adds hooks for developers to take advantage of the ability to track file upload progress in real time. This article is the fifth part of a series of five articles on new features in PHP V5.2. It will show you...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template