Jump to content

Recording file upload time


fahim_junoon

Recommended Posts

Hi all

 

I’ve created a form with which a user can upload files, for example images, to the server.

 

I would like to record how long it took php to upload the file to the server so that i can display this to the user.

 

I have tried using a timestamp before moving the file and then a timestamp afterwards but the timestamps are exactly the same.

 

Is there anyway to record how long it took php to write the file to the tempory directory?

 

Thanks

 

Link to comment
Share on other sites

Hi

 

Thanks for the link but it still isn't working how i want. Here is my code.

 

<?php

 

if (isset($_FILES['avatar']['tmp_name'])) {

 

$time_start = microtime(true);

 

$uploadfile = $_FILES['avatar']['tmp_name'];

$path = "testing/test1";

 

if (move_uploaded_file($uploadfile, $path)) {

echo 'File uploaded!<br />';

}

 

$time_end = microtime(true);

 

$time = $time_end - $time_start;

echo 'Script took '.$time.' seconds to exicute';

 

}

 

?>

 

<form action="" method="post" enctype="multipart/form-data"> 

<input type="file" name="avatar" />

<input type="submit" value="Upload" />

</form>

 

The problem is that it records how long the script took to execute which was 0.00161695480347 seconds.

 

I want it to tell me how long it took to transfer the file from the user's computer to the server e.g. 35 seconds.

 

I think php first uploads the file to the server before processing the if (isset($_FILES['avatar']['tmp_name'])) and so i cannot find out how long it took, only how long it took for the rest of the code including moving the temporary file to the new location.

Link to comment
Share on other sites

Nope still get the same time 0.00160503387451

 

I might just drop this for now as it’s not really a necessity for the system to work. it’s just that I was planning on creating a video upload form and it would be cool to know how long it took to upload a video and have that time stored in the database as an extra bit of info.

 

Thanks for the help

 

Link to comment
Share on other sites

Haven't tried this, but might work...

in the upload form have a hidden field with the current timestamp so when you submit (maybe use javascript for the onsubmit to enter the time so it will be more accurate), it sends that time to the server and after your move_uploaded_file() take another timestamp and compare.

Link to comment
Share on other sites

This is quite strange but when you put a hidden input field into the form either the browse button disappears if it is put before the file input or the upload button disappears if it is put after the file input. This happens in both ie7 and firefox.

 

<form action="" method="post" enctype="multipart/form-data">

<input type="file" name="avatar" />

<input type="hidden" name="time" value="<?php echo time(); ?> />

<input type="submit" value="Upload" />

</form>

 

 

Link to comment
Share on other sites

Sorry thanks for that. The hidden input method still didn’t work as expected though as the user could wait a minute before clicking upload and then the time taken to upload will say 1 minute 30 seconds even if it took 30 seconds. Here's the code i used.

 

<?php

 

if (isset($_FILES['avatar']['tmp_name'])) {

 

$start_time = $_POST['time'];

 

echo "Start time: $start_time<br />";

 

$uploadfile = $_FILES['avatar']['tmp_name'];

$path = "testing/test1";

 

if (move_uploaded_file($uploadfile, $path)) {

echo 'File uploaded!<br />';

$endtime = time();

echo "End time: $endtime";

}

 

$timetaken = $endtime - $start_time;

 

echo "Time Taken: $timetaken";

 

}

 

?>

 

 

Link to comment
Share on other sites

time() and microtime() may have different output types, but I'm not sure though.

 

 

Try something like this:

<script type="text/javascript">
function clearTime(){
     document.getElementById('time').value = parseInt(new Date().getTime().toString().substring(0, 10));
}
</script>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="avatar" />
<input type="hidden" name="time" id="time" />
<input type="submit" value="Upload" onclick="clearTime();" />
</form>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.