2017-10-04 152 views
0

我有一個函數,顯示上傳到特定網頁的所有文件的列表。我還想顯示文件上次修改的日期,但它顯示的所有日期都是1969年12月31日19:00:00。如何修改此功能以顯示正確的日期? (它在echo語句在結尾):上次修改日期不正確

<?php  
    foreach($phpfiles as $phpfile) 
    { 
     $imageFileType = pathinfo($phpfile,PATHINFO_EXTENSION); 
     if ($imageFileType == "pdf") { 
      $faicon = 'fa fa-file-pdf-o'; 
     } else if ($imageFileType == "doc" || $imageFileType == "docx") { 
      $faicon = 'fa fa-file-word-o'; 
     } else if ($imageFileType == "xls" || $imageFileType == "xlsx") { 
      $faicon = 'fa fa-file-excel-o'; 
     } else if ($imageFileType == "ppt" || $imageFileType == "pptx") { 
      $faicon = 'fa fa-file-powerpoint-o'; 
     } else if ($imageFileType == "mp4") { 
      $faicon = 'fa fa-file-video-o'; 
     } else if ($imageFileType == "jpg" || $imageFileType == "png" || $imageFileType == "gif" || $imageFileType == "bmp") { 
      $faicon = 'fa fa-file-image-o'; 
     } else { 
      $faicon = 'fa fa-file-text-o'; 
     } 
     if ($server_name <> ""){ 
      $phpfile = "http://". $server_name . '/' .$phpfile; 
     } 
     echo "<span class='$faicon w3-large'></span>&emsp;&emsp;<a href='$phpfile' class='w3-medium' target='_blank'>".substr(basename($phpfile),-1*strlen(basename($phpfile))+strlen($id)+1)."</a>&nbsp;". date("F d Y H:i:s.", filemtime($phpfile)); 
    } 
?> 

enter image description here

+0

你檢查這https://stackoverflow.com/questions/1853575/why-do-i-get-31-dec-1969-as-my-last-modified-filename-using-filemtime-在-php –

+0

我沒有,但在閱讀它後,我沒有得到任何錯誤建議。此外,我的文件正在填充,所以我知道它正在查找該文件。 – xxdash

+0

這會迴應'''file_exists($ phpfile)'''?要求根據你當前的代碼$ phpfile可能是有效的位置,所以pathinfo()將正常工作(它does not檢查存在的文件,只是解析字符串,因爲我記得),但沒有什麼會在文件系統 –

回答

1

如上評論:

的問題是,您覆蓋變量$ phpfile與此代碼:

if ($server_name <> ""){ 
    $phpfile = "http://". $server_name . '/' .$phpfile; 
} 

如果您在改變字符串$ phpfile之前改爲使用filemtime日期創建一個變量,那麼稍後可以回顯它。

$date = date("F d Y H:i:s.", filemtime($phpfile)); 
if ($server_name <> ""){ 
    $phpfile = "http://". $server_name . '/' .$phpfile; 
} 
echo "<span class='$faicon w3-large'></span>&emsp;&emsp;<a href='$phpfile' class='w3-medium' target='_blank'>".substr(basename($phpfile),-1*strlen(basename($phpfile))+strlen($id)+1)."</a>&nbsp;". $date;