tags:

views:

89

answers:

3

Hi friends, I want to find download time in PHP by sending a request to my MySQL server. I have one table with an href link to download the file. If I click download the download time should show beside the file name in a table. Can anyone please help me out? Today is my deadline. Please check out my code:

<?php
$con = mysql_connect("localhost","mt","mt");

mysql_select_db("mt", $con);
$d=date("d/m/Y H:i:s");
$result = mysql_query("SELECT * FROM mt_upload");
echo "<table BORDER=2 BORDERCOLOR=RED>
<tr>
<th>FileName</th>
<th>FilePath</th>
<th>Uploaded Date/Time</th><th>Download Date/Time</th>
</tr>";
while($row = mysql_fetch_array($result))
   {
echo
 "
<tr>

<td>". $row['FileName'] . "</td> 
 <td ><a  href=../sites/default/files/ourfiles/". $row['FileName']."  >Download</a></td>
<td>".$row['DateTime']."</td><td>$d</td>
</tr>" ;
  }
echo "</table>";
mysql_close($con);

?>
+2  A: 

Ok, now I understand the question based on OP's comment. The question is how to figure out what time did a user downloaded a file. If that's the case, the download link needs to be a php script, and it would write in the time into db, then returns the content of the file into the stream with proper content header.

See readfile.

<?php
$file = 'monkey.gif';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
?>

All you have to do is pass in the file name as some parameter and write the current time into DB.

eed3si9n
hi am begginer can u please send some cods i want at wat time user downloaded
Lavanya ks
If you are a beginner, you need to now learn how Web scripts work. Shortcuts are not the solution. Sorry, if you feel I am bitching around, but this is the first time I am doing this.
Manish Sinha
@Lavanya ks, just to clarify, you are asking for the time at which a user downloaded a given file, not how long it took for the user to download the file, correct?
eed3si9n
I don't think he needs that, he wants the total time taken for the file to download.
Manish Sinha
do logging the time the user has downloaded make any sense??? it all depend on the speed of the connection of the user....
RageZ
This doesn't make any sense to me. Sure, you can record the time the user *started* to download the file, but not when it finished. Not without some JavaScript (and probably some Flash or another technology too).
Mark
@Manish Sinha: can't do this. The browser doesn't give any indication at all when the download is finished. Not to the server, nor to the client (no DOM/JS event, no nothing). You could create a Flash or ActiveX custom downloader control (which would download the data *instead* of the browser), but that's a seriously ugly hack.
Piskvor
A: 

I am not very much sure, but I don't think it is possible via PHP.

When I open a PHP page, the webserver gives me the page. When I click on a link which say downloads xyz.abc file, then it is actually another request to the webserver and the current PHP page is not involved in this. The current PHP page is just hosting the link.

When I click on the link, the webserver finds the file and sends it to the client. It is the webserver which serves the page and it is the webserver which can keep track of the time since it knows when it has opened the socket for file download and when that socket got closed.

If anyone knows how to do it via a PHP script, please let me know too.

Manish Sinha
I think the answer below is correct - http://stackoverflow.com/questions/1598765/file-download-time-in-php-mysql/1598793#1598793
Manish Sinha
A: 

if you need some code you should use filesize and you can evaluate the time to download by doing some calculation, you cannot know the exact time since it depend on the user speed and all the network between your server and the user.

$filesize = filesize($yourfile);
$time_for_modem = $filesize * 8 / (56*1024);
$time_for_adsl = $filesize * 8 / (5*1024*1024);
$time_for_t3 = $filesize * 8 / (44*1024*1024);

function convertSecondToTime($sec){
  $hour = floor($sec/60)
  return $hour . 'hours and ' . ($sec%60) . 'minutes';
}

The modem time is for a 56kbps connection, adsl a 5 mbps, T3 44 mbps connection.

RageZ
The problem comes with the server not knowing at what speed the download is happening.
Manish Sinha
the server know at what speed the download is happening but it would be different for each client ... apart if you are saying you have only few client and you have access to the users machine that's a different problem.
RageZ
RagZ, I meant that server knows the time and it knows at what time it opened the socket for download and what time it closed it. Now the bigger problem is how the PHP script can know these stats from the webserver?
Manish Sinha
dumping the file using fread and echo would make possible to know the transfer rate (if the webserver doesn't use caching) but since it would change on an user to user basis does it is really useful ?
RageZ