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

Jul 13, 2016 pm 05:41 PM
hook php upload for Function develop document New track schedule

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

Explain the match expression (PHP 8 ) and how it differs from switch. Explain the match expression (PHP 8 ) and how it differs from switch. Apr 06, 2025 am 12:03 AM

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

See all articles