fahim_junoon Posted September 29, 2008 Share Posted September 29, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/126212-recording-file-upload-time/ Share on other sites More sharing options...
The Little Guy Posted September 29, 2008 Share Posted September 29, 2008 http://phpsnips.com/snippet.php?id=26 follow the comments, It should do what you want! Quote Link to comment https://forums.phpfreaks.com/topic/126212-recording-file-upload-time/#findComment-652666 Share on other sites More sharing options...
fahim_junoon Posted September 29, 2008 Author Share Posted September 29, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/126212-recording-file-upload-time/#findComment-652672 Share on other sites More sharing options...
The Little Guy Posted September 29, 2008 Share Posted September 29, 2008 place $time_start above the first if, see if that does anything. Quote Link to comment https://forums.phpfreaks.com/topic/126212-recording-file-upload-time/#findComment-652680 Share on other sites More sharing options...
fahim_junoon Posted September 29, 2008 Author Share Posted September 29, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/126212-recording-file-upload-time/#findComment-652685 Share on other sites More sharing options...
The Little Guy Posted September 29, 2008 Share Posted September 29, 2008 actually I don't thing that it actually uses the PHP to upload the file, I think the web form does that... Quote Link to comment https://forums.phpfreaks.com/topic/126212-recording-file-upload-time/#findComment-652686 Share on other sites More sharing options...
fahim_junoon Posted September 29, 2008 Author Share Posted September 29, 2008 Yea I think you’re right otherwise the code execution time should be longer than 0.0056 or whatever it was. Quote Link to comment https://forums.phpfreaks.com/topic/126212-recording-file-upload-time/#findComment-652688 Share on other sites More sharing options...
CroNiX Posted September 29, 2008 Share Posted September 29, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/126212-recording-file-upload-time/#findComment-652690 Share on other sites More sharing options...
fahim_junoon Posted September 29, 2008 Author Share Posted September 29, 2008 Thanks cronix i'll give it a try Quote Link to comment https://forums.phpfreaks.com/topic/126212-recording-file-upload-time/#findComment-652691 Share on other sites More sharing options...
fahim_junoon Posted September 29, 2008 Author Share Posted September 29, 2008 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> Quote Link to comment https://forums.phpfreaks.com/topic/126212-recording-file-upload-time/#findComment-652696 Share on other sites More sharing options...
The Little Guy Posted September 29, 2008 Share Posted September 29, 2008 you forgot a double quote after ?> Quote Link to comment https://forums.phpfreaks.com/topic/126212-recording-file-upload-time/#findComment-652698 Share on other sites More sharing options...
fahim_junoon Posted September 29, 2008 Author Share Posted September 29, 2008 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"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/126212-recording-file-upload-time/#findComment-652702 Share on other sites More sharing options...
jamesbrauman Posted September 29, 2008 Share Posted September 29, 2008 Try editing the hidden inputs value when the submit button is clicked. I dont know (hardly) any javascript, but something like this would run when the submit button is clicked. hiddeninputfield.value = javascriptfunctiontogettimestamp(); this.form.submit(); Quote Link to comment https://forums.phpfreaks.com/topic/126212-recording-file-upload-time/#findComment-652704 Share on other sites More sharing options...
fahim_junoon Posted September 29, 2008 Author Share Posted September 29, 2008 Yea that may work, only problem is I don’t know much JavaScript either. I’ll have a look and see if there is a function like php’s time() but in JavaScript so it will work properly. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/126212-recording-file-upload-time/#findComment-652707 Share on other sites More sharing options...
The Little Guy Posted September 29, 2008 Share Posted September 29, 2008 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> Quote Link to comment https://forums.phpfreaks.com/topic/126212-recording-file-upload-time/#findComment-652708 Share on other sites More sharing options...
fahim_junoon Posted September 29, 2008 Author Share Posted September 29, 2008 Thanks! It works great, it was only a second or two out during a 50 second upload when I tested it. Quote Link to comment https://forums.phpfreaks.com/topic/126212-recording-file-upload-time/#findComment-652713 Share on other sites More sharing options...
CroNiX Posted September 29, 2008 Share Posted September 29, 2008 cool, glad it worked Quote Link to comment https://forums.phpfreaks.com/topic/126212-recording-file-upload-time/#findComment-652715 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.