如何获取在 JavaScript/PHP 中上传的文件的修改时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7612894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the modified time of a file being uploaded in JavaScript/PHP?
提问by Kumar Kush
Is there ever a way possible to get the actualcreation / modification time of the file being uploaded, using JavaScript / PHP?
有没有办法使用 JavaScript/PHP 获取上传文件的实际创建/修改时间?
As for JavaScript, I have researched a lot, tried quite a few codes, but to no luck (perhaps trying to do possible out of impossible).
至于 JavaScript,我研究了很多,尝试了很多代码,但都没有运气(也许是在不可能的情况下尝试做可能的事情)。
As for PHP, using filectime()and filemtime(), it only shows the date / time the file is uploaded, and not the time the file is actually created / modified on the source.
至于 PHP,使用filectime ()和filemtime(),它只显示文件上传的日期/时间,而不是文件在源上实际创建/修改的时间。
In short, what I want is to check the m-time of a file before/during/after upload (where-ever possible) and decide whether or not to store the file on the server, and report the same back to the client.
简而言之,我想要的是在上传之前/期间/之后检查文件的 m 时间(尽可能)并决定是否将文件存储在服务器上,并将其报告给客户端。
回答by T.J. Crowder
If you're talking about the file date/time on the user's machine, you can get that via the File API(support), which provides lastModified
, which is the date/time as a number of milliseconds since The Epoch (if you want a Date
, you can pass that into new Date
). (There's also the deprecated lastModifiedDate
, but that is deprecated and not supported on Safari [at least].) The File API is universally supported in modern browsers (the particular feature you'd be using is the File
object). You'd get the value from the File
object and include that information in a separate (for instance, hidden) field.
如果您谈论的是用户机器上的文件日期/时间,您可以通过File API(支持)获得,它提供lastModified
了自 The Epoch 以来以毫秒为单位的日期/时间(如果您想要一个Date
,您可以将其传递给new Date
)。(还有 deprecated lastModifiedDate
,但 Safari 已弃用且不支持 [至少]。)现代浏览器普遍支持 File API(您将使用的特定功能是File
object)。您将从File
对象中获取值并将该信息包含在单独的(例如,隐藏的)字段中。
Here's a rough-but-complete example of reading the last modified date (live copy):
这是读取上次修改日期(实时副本)的粗略但完整的示例:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title>Show File Modified</title>
<style type='text/css'>
body {
font-family: sans-serif;
}
</style>
<script type='text/javascript'>
function showFileModified() {
var input, file;
// Testing for 'function' is more specific and correct, but doesn't work with Safari 6.x
if (typeof window.FileReader !== 'function' &&
typeof window.FileReader !== 'object') {
write("The file API isn't supported on this browser yet.");
return;
}
input = document.getElementById('filename');
if (!input) {
write("Um, couldn't find the filename element.");
}
else if (!input.files) {
write("This browser doesn't seem to support the `files` property of file inputs.");
}
else if (!input.files[0]) {
write("Please select a file before clicking 'Show Modified'");
}
else {
file = input.files[0];
write("The last modified date of file '" + file.name + "' is " + new Date(file.lastModified));
}
function write(msg) {
var p = document.createElement('p');
p.innerHTML = msg;
document.body.appendChild(p);
}
}
</script>
</head>
<body>
<form action='#' onsubmit="return false;">
<input type='file' id='filename'>
<input type='button' id='btnShowModified' value='Show Modified' onclick='showFileModified();'>
</form>
</body>
</html>
回答by Diodeus - James MacFarlane
JavaScript does not have access to the local filesystem, so you can't get to this information without using Flash, Java or Active-x.
JavaScript 无法访问本地文件系统,因此如果不使用 Flash、Java 或 Active-x,您将无法获得这些信息。
回答by Arduinerd
Perhaps you could use javascript to get the last modifiedtime, then use that in some other javacript to sort on that. This time will be in GMT.
也许您可以使用 javascript 来获取上次修改时间,然后在其他一些 javacript 中使用它来对其进行排序。这次将在格林威治标准时间。
var xmlhttp = createXMLHTTPObject();
xmlhttp.open("HEAD", "http://myurl/interesting_image.jpg" ,true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
alert("Last modified: "+
var lastModTimeForInterestingImage = xmlhttp.getResponseHeader("Last-Modified"))
}
}
xmlhttp.send(null);