PHP/MySQL 中的文件下载时间 [英] File download time in PHP/MySQL
问题描述
我想通过向我的 MySQL 服务器发送请求来查找 PHP 中的下载时间.我有一张带有 href 链接的表格,用于下载文件.如果我点击下载,下载时间应该显示在表格中的文件名旁边.
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.
请查看我的代码:
<?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);
推荐答案
好的,现在我根据 OP 的评论理解了这个问题.问题是如何确定用户下载文件的时间.如果是这样,下载链接需要是一个php脚本,它会在时间写入db,然后将文件内容返回到带有适当内容头的流中.
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.
请参阅读取文件.
<?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.
这篇关于PHP/MySQL 中的文件下载时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!