📜 ⬆️ ⬇️

Upload progress using PHP 5.4

Currently, there are many options for determining the progress of a file download: both with the help of client technologies and with the help of server technologies. An example of client technologies is swfupload using Flash, an example of server technologies is nginx_uploadprogress_module.
The upcoming release of PHP presents us with a gift in the form of a native tool for determining the progress of downloading a file.
We will deal with it.

PHP is still in the RC stage, but it will soon appear on dedicated servers, VDS and especially daring hosting. In any case, in the uncertain future, PHP 5.4 will become the main branch, and PHP 5.3 will be declared deprecated.
Let's start with the form:

<form action="" method="POST" enctype="multipart/form-data"> <input type="hidden" name="<?=ini_get("session.upload_progress.name")?>" value="uniqueValue" /><br /> <input type="file" name="file" /><br /> <input type="submit" /> </form> 

')
The only remarkable place in it is a hidden field with the name PHP_SESSION_UPLOAD_PROGRESS (which can be changed, therefore it is defined here through a function). And any unique value.

Place the form in the iframe on the target page.
And put our script to determine the percentage of loading:

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script> $(function() { setInterval(function() { $.get('?ajax', function(data) { $('#ajax').html(data); }); }, 500); }); </script> <div id="ajax"></div> 

For an asynchronous request, I used the jQuery library to reduce the code and improve its readability.
In this case, everything that we get from asynchronous requests, I put in a block with the identifier ajax.

And the last part is the server handler for the asynchronous request:

 if (isset($_SESSION["upload_progress_uniqueValue"])) { $progress = $_SESSION["upload_progress_uniqueValue"]; $percent = round(100 * $progress['bytes_processed'] / $progress['content_length']); echo "Upload progress: $percent%<br /><pre>" . print_r($progress, 1) . '</pre>'; } else { echo 'no uploading'; } 

This is a debug option, and it contains unnecessary data for the working draft. But we just want to look at them and also throw out in response.

We put it all on one page, the code is small (half a page) and therefore I will post it right here:
 <?php session_start(); $uploadName = 'test'; //   if (isset($_GET['ajax'])) { //   if (isset($_SESSION["upload_progress_$uploadName"])) { //      $progress = $_SESSION["upload_progress_$uploadName"]; $percent = round(100 * $progress['bytes_processed'] / $progress['content_length']); echo "Upload progress: $percent%<br /><pre>" . print_r($progress, 1) . '</pre>'; } else { echo 'no uploading'; } exit; } elseif (isset($_GET['frame'])) { //   ?> <form action="" method="POST" enctype="multipart/form-data"> <input type="hidden" name="<?=ini_get("session.upload_progress.name")?>" value="<?=$uploadName?>" /><br /> <input type="file" name="file" /><br /> <input type="submit" /> </form> <?php } else { ?> <iframe src="?frame" height="100" width="500"></iframe> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script> $(function() { setInterval(function() { //   $.get('?ajax', function(data) { //    $('#ajax').html(data); //     }); }, 500); }); </script> <div id="ajax"></div> <?php } 

Imposition is invalid - it is specially reduced for an example as much as possible.

Screenshot for clarity:


The article provides basic information that can be found in the documentation, but since this is not the case in current, up-to-date documentation, and even more so in books, I thought it would be useful for PHP developers.

Source: https://habr.com/ru/post/136974/


All Articles