Determine how long it takes to download a file (PHP)

Is it possible to find out (serveride) the time required to download a file? I have an image loading API, and in my answer I would like to return the loading time (not including the script runtime).

+4
source share
6 answers

I think yes, there is a variable $_SERVER['REQUEST_TIME'] that indicates the beginning of the HTTP request, so at the very beginning of your script:

 $upload_time = time() - $_SERVER['REQUEST_TIME']; 

The result will be in seconds.

+7
source

This method seems to work pretty fine:

  • Send an ajax request (right before the form posts) to the server that stores the session timestamp
  • Submit form
  • Check the difference in the receiving side :)

HTML

 <?php session_start(); // must be at the top ?> <form id="upload-form" enctype="multipart/form-data" method="post"> <input type="file" name="datafile" size="40"> <input type="submit" value="Send"> </form> <script type="text/javascript"> $(function() { $('#upload-form').submit(function() { $.ajax({ url: 'start-timer.php', type: 'POST', context: this, success: function() { this.submit(); }, }); return false; }); }); </script> 

start timer.php

 <?php session_start(); $_SESSION['time'] = microtime(true); 

upload.php

 <?php session_start(); header('content-type: text/plain; charset=utf-8'); if( ! isset($_FILES['file']) || ! isset($_SESSION['time']) || $_FILES['file']['error'] !== UPLOAD_ERR_OK || ! is_uploaded_file($_FILES['file']['tmp_name'])) { exit('stuff went wrong...'); } $time = microtime(true) - $_SESSION['time']; unset($_SESSION['time']); echo round($time, 3).'s'; 

Working example: http://samples.geekality.net/upload-timer

+5
source

You simply cannot measure anything inside the receiver of the script.
Just because it starts immediately after the download is completed (processed by the web server).

So, as Svish said, calling an AJAX and timestamp in a session would be the easiest solution.

+2
source

Look at the link.

http://prefetch.net/blog/index.php/2007/01/02/measuring-apache-request-processing-time/

It shows how to configure apache to record the time the request receives apache.

Then you can use the apache_request_headers foundation in PHP and extract the time from it.

http://php.net/manual/en/function.apache-request-headers.php

Perhaps this way you can figure out the time between when apache starts receiving the file and when it passes processing to php?

You might need to do some testing as I have not tried this. Let me know if this works ... :)

UPDATE @GustavBertram :

Note. I edited the answer instead of my own question, as I want to document the attempt separately for each answer.

I enabled mod_headers in Apache and added the following configuration file:

 #/etc/apache2/mods-available/headers.conf RequestHeader set X-Request-Received: %t 

Then I updated my script:

 <form action="#" enctype="multipart/form-data" method="post"> Upload:<input type="file" name="datafile" size="40"> <input type="submit" value="Send"> </form> <?php // Get the request headers $REQUEST_HEADERS = apache_request_headers(); // Extract $t value from header parse_str($REQUEST_HEADERS['X-Request-Received']); // Massage string to get float in seconds $received_time = substr_replace($t, '.', 10, 0); // Get the current microtime as float in seconds $current_time = microtime(1); $upload_time = $current_time - $received_time; echo $received_time . " \n <BR />"; echo $current_time . " \n <BR />"; echo $upload_time; 

I tested it both on my local computer and on a remote computer on the network, with a file of 700 MB.

+1
source

In Apache 2.2.22 and PHP 5.2.17 I use the apache_response_headers () command with the flush () command, otherwise it does not work, I get a header with one variable. ob_end_flush () did not help me. The REQUEST_TIME of the $ _SSERVER variable is an elegant equivalent, but note that this works with PHP 5.1.

0
source

I mean simple logic

when the image was sent for upload

save the start time in a variable and set another variable, for example. uploadStatus = 0, then when the download is complete, set the variable uploadStatus = 1, and also save the end time in another variable.

When downloading, select the uploadStatus == 1 checkbox, and then subtract the start time from the end and you will get the download time.

-2
source

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


All Articles