How to get the modified time of a file being uploa

2019-01-14 19:31发布

Is there ever a way possible to get the actual creation / modification time of the file being uploaded, using 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).

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.

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.

3条回答
乱世女痞
2楼-- · 2019-01-14 19:50

Perhaps you could use javascript to get the last modified time, then use that in some other javacript to sort on that. This time will be in GMT.

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);
查看更多
姐就是有狂的资本
3楼-- · 2019-01-14 20:03

If you're talking about the file date/time on the user's machine (e.g., client-side), your only option other than going down a proprietary path (ActiveX — which would be a very unpleasant user experience with warnings making many users run for the hills — Flash, etc.) is the relatively-new, not-widely-supported File API, which provides the lastModifiedDate. But again, you'd have to detect whether the browser supported it, and then include that information in a separate (for instance, hidden) field.

When I say the file API isn't widely supported yet, actually that depends on your point of view: Firefox, Chrome, and Opera do support it in recent versions (Firefox for a long time, it was their idea). Apparently IE doesn't support it yet, not even in IE9 (I haven't personally verified that).

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 " + file.lastModifiedDate);
        }

        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>
查看更多
We Are One
4楼-- · 2019-01-14 20:11

JavaScript does not have access to the local filesystem, so you can't get to this information without using Flash, Java or Active-x.

查看更多
登录 后发表回答