如何获取在JavaScript / PHP中上传文件的修改时间? [英] How to get the modified time of a file being uploaded in JavaScript/PHP?

查看:199
本文介绍了如何获取在JavaScript / PHP中上传文件的修改时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能使用JavaScript / PHP获取正在上传的文件的实际创建/修改时间?

Is there ever a way possible to get the actual creation / modification time of the file being uploaded, using JavaScript / PHP?

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

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).

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

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.

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

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.

推荐答案

如果你在谈论文件日期/时间在用户的计算机上,您可以通过文件API support ),它提供了 lastModified ,这是自The Epoch以来的一个毫秒数的日期/时间(如果你想要一个日期,您可以将其传递到新日期)。 (还有被弃用的 lastModifiedDate ,但已弃用且不支持Safari [至少]。)现代浏览器普遍支持File API(您使用的特定功能是 文件对象)。您将从文件对象中获取值,并将该信息包含在单独的(例如,隐藏)字段中。

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.

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

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>

这篇关于如何获取在JavaScript / PHP中上传文件的修改时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆