2012-10-03 77 views
2

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

有没有办法添加一些东西到PHP​​指定如果没有列出的文件夹打印此

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

下面是我使用的页面中的PHP:如果你需要再代码,如完整的网页HTML或CSS只是让我知道

<?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>"; 
?> 

在此先感谢您的帮助。

编辑:

试图Josh的解决方案它几乎钉,但现在我就在后“没有发现文件”打印3次。以下是我现在使用的代码:

<?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>"; 
?> 

回答

2

可以使用count函数来检查是否有您的文件阵列像这样的文件:

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!"; 

编辑:

你也可以使用scandir功能。但是,此功能将返回两个额外的条目当前目录目录上一级。您需要从文件阵列中删除这些条目。您的代码看起来就像这样:

<?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 
} 
?> 
+1

小心向代码添加一些注释,为什么你提出这个解决方案?怎么运行的?一些链接也 – Yaroslav

+0

感谢您的答案,但是当我实现它时出现错误,_Parse错误:语法错误,意外的'$ file'(T_VARIABLE)在C:\ filepath \ A2.php在线53_ – number8pie

+0

@Yaroslav I编辑我的答案使它更有帮助。感谢您指出了这一点。 – imlokesh

6

只是做一个:

if(count($files) == 0) 
{ 
    echo '<p>No files found</p>'; 
} 
else 
{ 
    // you have files 
} 
+0

你也可以做一个:如果(空($文件)){回声“

没有文件找到

”; }其他{//你有文件} –

+0

非常感谢,它的工作原理,但它打印出“找不到文件”三次。如果没有找到文件,有什么办法可以杀死循环?我已经更改了原始帖子,向您展示了我的代码现在的样子。再次感谢乔希。 – number8pie

+1

当然你必须在这之后做出来。 –

2

试试这个:

<?php 
$dir=opendir("."); 
$files=array(); 
while (($file=readdir($dir)) !== false) 
{ 
    if ($file != "." and $file != ".." and $file != "A.php") 
    { 
     array_push($files, $file); 
    } 
} 

closedir($dir); 

if(count($files) == 0){ 
    die("There are no records matching those criteria."); 
}else{ 
    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>"; 
} 

?> 
+0

感谢Eswar的回答,但是当文件夹中没有文件(与条件匹配)时代码购买了错误。这里是错误代码:_Parse错误:语法错误,意外的'$ file'(T_VARIABLE)在C:\ filepath \ A2.php在线53_ – number8pie

+0

请再次检查代码。我跑了它,我没有任何错误。 –

+0

我试过了代码,它工作得很好,谢谢Eswar。 – number8pie

0

您可以将您的文件阵列上使用count使用一个简单的条件。

// ... 
closedir($dir); 
if (count($files) > 0) { 
    // sort files and iterate through file array, printing html 
} else { 
    echo "There are no records matching those criteria."; 
} 
// ... 
+0

感谢您的答案,但它买了一个错误:_Parse错误:语法错误,意外的'$文件'(T_VARIABLE)在C:\文件路径\ A2.php行53_这可能是我的方式实现代码,我不确定像我提到的我是一个完整的PHP新手。 – number8pie

0

我认为你可以让它打印别的东西if($files.length == 0),只打印正常if($files.length > 0)什么的。

我不知道php,但我知道java,html,css和javascript。 我敢肯定,PHP有像array.length(或其他可以获得数组长度的东西)在里面

我希望这是有帮助的。

编辑:我见过其他人已经回答了比我的好10倍的thigs。 此外,PHP显得很酷,我可以了解它

+0

PHP看起来很棒,但我在学习第一个PHP或jQuery的时候感到厌烦。我倾向于jQuery,但PHP是如此。无论如何,感谢您的帮助。 – number8pie

+0

我建议首先使用PHP。无论您使用简单的Javascript还是浏览器中的jQuery增强功能,您几乎都需要执行服务器端脚本。你可以写出没有任何Javascript的合理的好页面。 – Barmar

+0

我认为你是对的@Barmar,有很多插件可以做各种各样的技巧,可以自由使用,直到我开始使用PHP。感谢您的建议。 – number8pie