2017-10-04 78 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;