search
HomeBackend DevelopmentPHP TutorialPHP file upload progress tracking PHP5.2 has added this feature_PHP tutorial

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 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></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 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></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 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 class="tag-name">br</span><span class="tag">/></span><span> </span></span></span></li>
<li class="alt"><span> </span></li>
<li><span class="tag"><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></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>

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
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
PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

PHP Email Security: Best Practices for Sending EmailsPHP Email Security: Best Practices for Sending EmailsMay 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

See all articles

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

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software