如何获取在JavaScript/PHP中上传文件的修改时间?

kus*_*tus 8 javascript time file

有没有办法使用JavaScript/PHP获得正在上传的文件的实际创建/修改时间?

至于JavaScript,我已经研究了很多,尝试了很多代码,但没有运气(或许试图做出可能的不可能).

至于PHP,使用filectime ()filemtime(),它只显示文件上传的日期/时间,而不是文件在源上实际创建/修改的时间.

简而言之,我想要的是在上传之前/期间/之后(尽可能地)检查文件的m时间,并决定是否将文件存储在服务器上,并将其报告回客户端.

T.J*_*der 13

如果您正在谈论用户计算机上的文件日期/时间,您可以通过文件API(支持)获取,它提供lastModified的日期/时间为自The Epoch以来的毫秒数(如果您需要Date,你可以把它传递给new Date).(也有不赞成使用lastModifiedDate,但不推荐使用,并且不支持Safari [至少].)现代浏览器普遍支持File API(您将使用的特定功能是File对象).您将从File对象获取值并将该信息包含在单独的(例如,隐藏)字段中.

这是阅读上次修改日期(实时副本)的粗略但完整的示例:

<!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>
Run Code Online (Sandbox Code Playgroud)


Dio*_*ane -2

JavaScript 无法访问本地文件系统,因此如果不使用 Flash、Java 或 Active-x,则无法获取此信息。