2011-09-30 142 views
3

有没有办法通过JavaScript/PHP获取上传文件的创建/修改时间实际?对于JavaScript,我研究了很多,尝试了很多代码,但没有运气(也许试图做不可能的事情)。如何获取以JavaScript/PHP上传文件的修改时间?

至于PHP,使用filectime()filemtime(),它仅示出了日期/时间的文件已上载,并且没有被修改的源极上被实际创建该文件的时间/。

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

+0

你在哪里上传使用什么方法的文件? –

回答

0

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

+0

*“JavaScript无法访问本地文件系统”*它在一些浏览器上,如Chrome,Opera,Firefox ...... –

+0

感谢Diodeus,Flash似乎是一个不错的选择。我会看看它是否符合我的逻辑。我必须实际上传同一页面上的多个文件以及m-time。 –

8

如果你在谈论的文件的日期/时间用户的机器(例如,客户端),你比下降专有的路径(的ActiveX   —其他唯一的选择上这将是一个非常不愉快的用户使用许多用户在山上跑步的警告经验  —闪存等)是相对较新的,未得到广泛支持的File API,它提供了lastModifiedDate。但是,您必须再次检测浏览器是否支持它,然后将这些信息包含在单独的(例如,隐藏的)字段中。实际上,这取决于你的观点:Firefox,Chrome和Opera在最近的版本中支持它(Firefox很长一段时间,这是他们的想法) 。 Apparently IE目前还不支持它,即使在IE9中(我没有亲自验证过)。

这里是读书的最后修改日期(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> 
+0

感谢Crowder,您的示例在Chrome中运行。这真的很容易。我的客户的用户将只使用IE浏览器,IE6或可能是IE7,而不仅限于此。所以只在IE中测试了我自己的代码。顺便说一句,你知道任何手机浏览器都支持这个API。这比IE(或者说PC)更重要。任何方式,再次感谢。 –

-1

也许你可以使用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); 
相关问题