使用PHP显示文件夹内容,但在文件夹为空时显示消息 [英] Using PHP to display a folders contents, but show a message when folder is empty

查看:70
本文介绍了使用PHP显示文件夹内容,但在文件夹为空时显示消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的工作场所创建了一个内部网,并使用了一些我在网上找到的php来扫描它所在的文件夹的内容并将它们显示为链接。它做得很好,但是当它在一个空文件夹中时,我希望它显示一条消息,比如没有匹配这些条件的记录。。

一种方法来添加一些东西到PHP​​指定如果没有列出的文件夹打印这个



我有旁边没有知识的PHP,但HTML和CSS是没有问题的。



以下是我在页面中使用的php:

 <?php 
$ dir = opendir(。);
$ files = array(); ($ file = readdir($ dir))!== false)
if($ file!=。和$ file!=..和$ file!=A.php)
{
array_push($ files,$ file);
}
}
closedir($ dir);
sort($ files);
foreach($ files as $ file)
print< div class ='fileicon'>
< a href ='$ file'>
< img src ='.. / .. / .. / images / TR-Icon.png'>
< p class ='filetext'> $ file< / p>
< / a> ;
< / div>;
?>

如果您需要更多代码,例如完整页面html或css,请告诉我。



预先感谢您提供任何帮助。

编辑:

在尝试Josh的解决方案后,它几乎已经钉上了它,但我现在正在没有找到文件打印3次。这里是我现在使用的代码:

 <?php 
$ dir = opendir(。) ;
$ files = array();
while(($ file = readdir($ dir))!== false)
{
if(count($ files)== 0)
{
echo'< p>没有找到文件< / p>';
}
else
{
if($ file!=。and $ file!=..and $ file!=A.php)
{
array_push($ files,$ file);
}
}
}
closedir($ dir);
sort($ files);
foreach($ files as $ file)
print< div class ='fileicon'>
< a href ='$ file'>
< img src ='.. / .. / .. / images / TR-Icon.png'>
< p class ='filetext'> $ file< / p>
< / a> ;
< / div>;
?>


解决方案

您可以使用 count 函数检查文件数组中是否有文件如下所示:

  if(count($ files)> 0)//检查文件数组中是否有文件
foreach($ files as $ file )//在条件为真时打印文件
print< a href ='$ file'> $ file< / a>< br />;
else
echoERROR!;

编辑:

您也可以使用 scandir 功能。但是,这个函数会将当前目录目录的两个额外条目返回到一个级别。您需要从文件阵列中删除这些条目。您的代码如下所示:

 <?php 
$ dir =。; //你要检查的目录
$ exclude = array(。,..); //你不希望这些条目在你的文件数组中
$ files = scandir($ dir);
$ files = array_diff($ files,$ exclude); //从文件数组中删除排除数组中的条目
if(!empty($ files))//检查文件数组是否为空
{
foreach($ files as $文件)//打印文件数组中的每个文件
print< div class ='fileicon'>
< a href ='$ file'>
< img src ='.. / .. / .. / images / TR-Icon.png'>
< p class ='filetext'> $ file< / p>
< / a>
< / div>;
}
else
{
echo目录中没有文件; //打印错误信息,如果有noe文件
}
?>


I'm creating a intranet for my workplace and have used a bit of php I found online to scan the contents of the folder it's in and display them as links. It does this fine, but when it's inside an empty folder I would like it to display a message such as "There are no records matching those criteria.".

Is there a way to add something to the php to specify if there are no folders listed print this?

I have next to no knowledge of php, but html and css are no problem.

Here's the php I'm using in the page:

<?php
$dir=opendir(".");
$files=array();
while (($file=readdir($dir)) !== false)
{
    if ($file != "." and $file != ".." and $file != "A.php")
    {
        array_push($files, $file);
    }
}
closedir($dir);
sort($files);
foreach ($files as $file)
print "<div class='fileicon'>
           <a href='$file'>
               <img src='../../../images/TR-Icon.png'>
               <p class='filetext'>$file</p>
           </a>
      </div>";
?>

If you need anymore code such as the full page html or css just let me know.

Thanks in advance for any help.

EDIT:

After trying Josh's solution it pretty much nailed it, but I'm now getting "No files found" printing 3 times. Here's the code I'm using now:

<?php
$dir=opendir(".");
$files=array();
while (($file=readdir($dir)) !== false)
{   
if( count($files) == 0 )
{
echo '<p>No files found</p>';
}
else
{
if ($file != "." and $file != ".." and $file != "A.php")
{
array_push($files, $file);
}
}
}
closedir($dir);
sort($files);
foreach ($files as $file)
print " <div class='fileicon'>
<a href='$file'>
<img src='../../../images/TR-Icon.png'>
<p class='filetext'>$file</p>
</a>
</div>";
?>

解决方案

You can use the count function to check if there are any files in your files array like this:

if(count($files) > 0) // check if there are any files in the files array
    foreach ($files as $file) // print the files if condition is true
        print " <a href='$file'>$file</a> <br />";
else 
    echo "ERROR!";

EDIT:

You can also use the scandir function. However, this function will return two extra entries for the current directory and directory up one level. You need to remove these entries from the files array. Your code will look like this:

<?php
$dir   = "."; // the directory you want to check
$exclude = array(".", ".."); // you don't want these entries in your files array
$files = scandir($dir);
$files = array_diff($files, $exclude); // delete the entries in exclude array from your files array
if(!empty($files)) // check if the files array is not empty
{
    foreach ($files as $file) // print every file in the files array
        print " <div class='fileicon'>
<a href='$file'>
<img src='../../../images/TR-Icon.png'>
<p class='filetext'>$file</p>
</a>
</div>";
}
else 
{
    echo "There are no files in directory"; // print error message if there are noe files
}
?>

这篇关于使用PHP显示文件夹内容,但在文件夹为空时显示消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆