2011-09-30 52 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); 
相關問題