我想要一个 HTML 表单来由用户将文件发送到我的服务器。我需要知道他开始发送此文件的确切时间 - 而不是接收文件的时间(我可以检查接收时间,例如通过检查服务器上的文件修改时间)。此代码应该执行此操作 - 单击“提交”时,当前服务器时间应写入logsForSendingForm.txt,文件接收时间应写入logsForReceivedForm.txt。 不幸的是,发送文件时,仅将接收文件的时间写入logsForReceivedForm.txt - 没有任何内容写入logsForReceivedForm.txt。 有趣的是,如果我不选择任何文件并单击“提交”,当前时间就会写入这两个文件。
如果你不知道如何调试这个,但你可以建议任何其他解决方案(也许没有AJAX),也没关系,我不需要像这段代码那样这样做。
<?php
if (isset($_POST['tickStart']))
{
file_put_contents('logsForSendingForm.txt', time() . "\n", FILE_APPEND);
}
elseif (!empty($_FILES))
{
file_put_contents('logsForReceivedForm.txt', time() . "\n", FILE_APPEND);
$f = $_FILES['file'];
$patch = str_replace('index.php', '', $_SERVER['SCRIPT_FILENAME']);
copy($f['tmp_name'], $patch.$f['name']);
}
?><!DOCTYPE html>
<html>
<head>
<script src='http://code.jquery.com/jquery-2.1.0.min.js'></script>
<script type='text/javascript'>
function sending()
{
$.ajax({
url: 'index.php',
type: 'POST',
data: { tickStart: true }
});
return true;
}
</script>
</head>
<body>
<form action='index.php' method='post' enctype='multipart/form-data'>
<input type='file' name='file'><br>
<input type='submit' name='submit' value='Submit' onsubmit="return sending()">
</form>
</body>
</html>
最佳答案
您需要进行更多检查,但这里有一些经过简要测试的代码。我使用 microtime() 而不是 time,因为对于小文件,我的本地主机上的秒数没有差异。我还添加了 here 中的一些内容例如,帮助通知用户他们的文件太大。您可能想根据 mime 类型进行捕获并通知他们...
我抛弃了 Jquery,因为它看起来多余。
如果(以及当)多个客户端尝试写入您的日志时,您可能仍然会收到损坏的文件。 (这就是为什么我在你的附加中添加了 | LOCK_EX 标志。我没有做过任何负载测试,所以不能保证。)数据库???
否则,您可能还需要进行一些文件名规范化以消除非法/非 ascii 字符。但这是另一个已在其他地方讨论过的问题。
干杯。
编辑: 持续时间:0.084668874740601(对于本地主机上的 23mb 文件) 持续时间:0.0021710395812988(对于本地主机上的 74k 文件)
<?php
if (isset($_POST['tickStart']))
{
// this is the moment the script began
$mtime1=$_SERVER['REQUEST_TIME_FLOAT'];
$log = 'sent: ' . $mtime1;
}
if(isset($_SERVER['REQUEST_METHOD']) && strtolower($_SERVER['REQUEST_METHOD']) == 'post' && $_FILES['file']['size'] == 0){
$postMax = ini_get('post_max_size');
$fileMax = ini_get('upload_max_filesize');
$message = "Max filesize: $fileMax<br>Max postsize: $postMax";
$log = 'file too large';
}
elseif (!empty($_FILES) && !empty($_POST) && $_FILES['file']['size'] > 0)
{
$f = $_FILES['file'];
$patch = str_replace('index.php', '', $_SERVER['SCRIPT_FILENAME']);
copy($f['tmp_name'], $patch.$f['name']);
// this is the time NOW
$mtime2=microtime(true);
file_put_contents('logsForSendingForm.txt', $mtime1 . "\n", FILE_APPEND | LOCK_EX);
file_put_contents('logsForReceivedForm.txt', $mtime2 . "\n", FILE_APPEND | LOCK_EX);
$duration = $mtime2 - $mtime1;
$log = $log . '\nduration: '.$duration;
$message = $f['name'].' uploaded.';
}
else
{
$log = 'no file selected';
$message = 'Please choose a file.';
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<script type='text/javascript'>
console.log('<?php print $log ?>');
</script>
</head>
<body>
<form action='index.php' enctype='multipart/form-data' method='POST'>
<input type='file' name='file'><br>
<input type='hidden' value='true' name='tickStart'>
<input type='submit' name='submit' value='Submit'>
</form>
<h2>
<?php print $message; ?>
</h2>
</body>
</html>
关于javascript - 检查表单提交文件的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23338673/