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:
<?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>
<?php session_start();
$_SESSION['time'] = microtime(true);
<?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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With