Measure upload time and speed with PHP and Javascript

Stumbled upon a question on StackOverflow the other day which got me curious. The question was about how to measure how long it takes to upload a file to a PHP script. This is what had been tried out:

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

This pretty much always returns zero, even though the uploading actually took many seconds, because the request start time is after the server has received the post data. That we actually just get how long the script took to run, which of course is pretty close to zero seconds. So, what can we do?

  1. Right before data is posted, nudge the server with an AJAX call which stores the current timestamp in a session variable
  2. Post the data
  3. Compare current timestamp with the one stored in step 1

Wasn’t sure how it would work, but seems to work pretty well. There will of course be a very tiny difference since the AJAX request will be a bit part of the time, but compared to the upload time it shouldn’t matter much. Anyways, here’s how you could do it 🙂

In your HTML you need your form, obviously.

<form action="upload.php" method="post" enctype="multipart/form-data">
  <input type="file" name="file" />
  <input type="submit" value="Upload" />

And then we need the Javascript which does the AJAX request (using jQuery). Notice that this makes sure we have gotten a response from the timer script before we actually submit the form.

<script type="text/javascript">
        url: 'start-timer.php',
        type: 'POST',
        context: this,
        success: function() { this.submit(); },
      return false;

The start timer simply stores a timestamp in a session variable.

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

Then in the upload script we simply need to compare that time with the current timestamp to figure out how long this took.

$upload_time = microtime(true) - $_SESSION['time'];
// You should of course probably also check if the upload was OK and all that ;)

And that’s pretty much all there is to it! Working sample at

  • Parth

    I’ve have this exact code running on my server, but when I run firebug on my html page it errors saying that ‘$ is not defined $(function)

    • u need jquery js-library. go to to download it

  • Mark

    There are a few file upload plug-ins that can be downloaded, but I wanted to figure out how the process works myself. After a short Googling session, I found the Stack Overflow article, which led me to your blog post. Thanks!

    I would also like to implement a real-time (or near-real-time) visual indication of the upload status (time left and/or percentage complete). I know part of that would be a matter of comparing the current size of the file on the server to the original file size (I’m assuming updating the time left to upload would be done in a similar fashion), but I have a couple of questions: (1) how to get the estimated time to upload (which can change drastically as the file is being uploaded), and (2) how would all of that be implemented in the context of your code?

    • Not sure how you’d do this because you’ll need to know the total size of the upload as well as how much has been uploaded. If you somehow can get these two variables on the server side you can poll them using AJAX. You can then use the start time and current time to calculate the average speed of the upload so far, and then calculate how much time is left assuming it will continue at roughly that same average speed. All fairly simple math really.

      Main problem is to get those two sizes. Total file size and how much has been uploaded so far. Let me know if you figure it out 🙂