简体   繁体   English

显示添加到网站目录的最新文件,同时排除PHP中的某些特定文件

[英]Show Newest File Added To Website Directory While Excluding Some Specific Files in PHP

Found this code online that was very helpful. 在网上发现此代码非常有帮助。 I have been trying to tweak it for a few hours now to exclude a certain file name of (mobile.htm). 我一直在尝试调整它几个小时,以排除某个文件名(mobile.htm)。 Can I get some help in the right direction to exclude the mobile.htm file name before the result is displayed and how can i display the full path to the file so I can turn it into a link? 我可以在正确的方向上获得一些帮助,以在显示结果之前排除mobile.htm文件名,以及如何显示文件的完整路径,以便可以将其转换为链接?

<?php
$path = "states"; 
$latest_ctime = 0;
$latest_filename = '';    
$d = dir($path);
while (false !== ($entry = $d->read())) {
   $filepath = "{$path}/{$entry}";
   if(is_file($filepath) && filectime($filepath) > $latest_ctime) {
      $latest_ctime = filectime($filepath);
      $latest_filename = $entry;
      echo $latest_filename;
?>

UPDATE: This is the code I got to work for me. 更新:这是我要为我工作的代码。 I had $var that I was trying to view on an index page that wasnt being seen. 我有$ var试图在未显示的索引页上查看。 What worked for me was making a iframe on the homepage that linked to another page that carried the $_SESSION info of the $var. 对我有用的是在首页上建立一个iframe,该iframe链接到另一个包含$ var的$ _SESSION信息的页面。 Here is the code that i placed int he iframe that worked for me. 这是我放置在对我有用的iframe中的代码。

The 3 if ($entry == "YourFile.html") {continue;} that I have listed stop the request from showing specific files you enter. 我列出的3 if($ entry ==“ YourFile.html”){continue;}阻止了请求显示您输入的特定文件。

<?php
$path = "states/$ls"; 
$latest_ctime = 0;
$latest_filename = '';
$d = dir($path);
while ($entry = $d->read()) {
if ($entry == "mobile.htm") {continue;}
if ($entry == "bg.jpg") {continue;}
if ($entry == "flag.png") {continue;}
$filepath = "$path/$entry";
$ctime = filectime($filepath);
if (is_file($filepath) && $ctime > $latest_ctime) {
    $latest_ctime = $ctime;
    $latest_filename = $filepath;
}
}
echo $latest_filename;
?>

A huge thank you to @Barmar below for helping me. 非常感谢下面的@Barmar帮助我。 I absolutely love this website and community. 我绝对喜欢这个网站和社区。

You can use a continue; 您可以使用continue; statement to skip to the next iteration of the loop. 语句以跳到循环的下一个迭代。

while ($entry = $d->read()) {
    if ($entry == "mobile.html") {
        continue;
    }
    $filepath = "$path/$entry";
    $ctime = filectime($filepath);
    if (is_file($filepath) && $ctime > $latest_ctime) {
        $latest_ctime = $ctime;
        $latest_filename = $filepath;
    }
}
echo $latest_filename;

I put $filepath in $latest_filename since you want the full path for your link. 我将$filepath放在$latest_filename因为您需要链接的完整路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM