开发者

Detect how long it takes for a file to upload (PHP)

开发者 https://www.devze.com 2023-01-02 00:41 出处:网络
Is it possible to know (serverside) the time it took for a file to 开发者_开发知识库upload? I have an image upload API and in my response I\'d like to return the upload time (not including script exec

Is it possible to know (serverside) the time it took for a file to 开发者_开发知识库upload? I have an image upload API and in my response I'd like to return the upload time (not including script execution time).


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

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

Result will be in seconds.


Seems this method works pretty OK actually:

  1. Send off an ajax request (right before form posts) to the server which stores a session timestamp
  2. Post the form
  3. Check difference in the receiving end :)

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 sample: http://samples.geekality.net/upload-timer


You simply can't measure anything inside the receiving script.
Just because it is started right after the upload (handled by the web-server) ends.

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


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 log time time the request is receieved by apache.

Then you can use the apache_request_headers fundtion in PHP and extract out that time.

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

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

Might need to do some testing, as I have not tried this. Let me know if it works... :)

UPDATE BY @GustavBertram:

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

I enabled mod_headers in Apache, and added the following config file:

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

I then 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 machine, and on a remote machine in the network, with a 700MB file.


At Apache 2.2.22 and PHP 5.2.17, I use apache_response_headers() lead with flush() command, another way it does not work, i receive header with one variable. ob_end_flush() did not help me. REQUEST_TIME variable of $_SSERVER is elegant equivalent but pay attention this works from PHP 5.1.


I have a simple logic in mind

when image is posted for upload

save starting time in a variable and set another variable for e.g. uploadStatus =0, then when upload is done set the variable to uploadStatus = 1 and also save the end time in another variable.

On upload check if uploadStatus == 1, then subtract the starting time from end time and you will get the upload time.

0

精彩评论

暂无评论...
验证码 换一张
取 消