我正在努力学习我的php脚本,我用这个作为参考:http://www.dynamicdrive.com/forums/showthread.php?60691-Listing-file-in-a-directory-problem。
我已经对它进行了调整以适应我,但是我似乎无法解决如何解决这个PHP错误:
PHP警告:sort()期望参数1为数组,给定
为null
我理解错误在第83行
排序($ dirArray);
但是我不知道如何修复错误,因为我希望禁止的文件功能保持原样,如果我改变它:
// Gets each entry
while($entryName = readdir($myDirectory))
{
if (is_file($entryName))
{
$extensions = explode(".", $entryName);
if(!in_array($extensions[1],$forbiddenExtensions))
{
$dirArray[] = $entryName;
}
}
}
对此:
while($entryName = readdir($myDirectory))
{
$dirArray[] = $entryName;
}
我知道会没事的。我100%肯定他们的一个简单的解决方案,我的大脑只是炒了我不能再思考156次重新编辑,重新上传,我需要帮助。有人可以帮我找一下我的$ forbiddenExtensions的解决方法吗?
<?php
// Open this directory
// (NOTE: To open same folder as this file use($directory = "./";)
$directory = "./files/";
// Selects $directory to open
$myDirectory = opendir($directory);
// Sets forbidden extensions
$forbiddenExtensions = array("php", "html", "log");
// Gets each entry
while($entryName = readdir($myDirectory))
{
if (is_file($entryName))
{
$extensions = explode(".", $entryName);
if(!in_array($extensions[1],$forbiddenExtensions))
{
$dirArray[] = $entryName;
}
}
}
// This function separates the extension from the rest of the file name and returns it
function findExtensions ($filename)
{
$filename = strtolower($filename) ;
$extensions = explode(".", $filename) ; //$extensions = split("[/\\.]", $filename) ;
$n = count($extensions)-1;
$extensions = $extensions[$n];
return $extensions;
}
// Closes $directory
closedir($myDirectory);
// Counts elements in array
$indexCount = count($dirArray);
Print ("$indexCount files<br>\n"); //Display how many files were counted
// Sorts the Array of files
sort($dirArray);
// Prints the Array of files
print("<table width='100%' cellspacing='10'>
<tr>
<td class='database_head'>Filename</td>
<td class='database_head'>Filetype</td>
<td class='database_head'>Filesize</td>
</tr>\n");
// Loops through the array of files and display them all
for($index=0; $index < $indexCount; $index++)
{
// Don't list hidden files
if (substr($dirArray[$index], 0, 1) != ".")
{
print("<tr><td><a href=\"$dirArray[$index]\">$dirArray[$index]</a></td>");
print("<td>");
print(findExtensions($dirArray[$index]));
print("</td>");
print("<td>");
print(filesize($directory.$dirArray[$index]));
print("</td>");
print("</tr>\n");
}
}
print("</table>\n");
?>
答案 0 :(得分:0)
试试这个:
<?php
$i=0;
$dirArray = array();
// Open this directory
// (NOTE: To open same folder as this file use($directory = "./";)
$directory = "./files/";
// Selects $directory to open
$myDirectory = opendir($directory);
// Sets forbidden extensions
$forbiddenExtensions = array("php", "html", "log");
// Gets each entry
while($entryName = readdir($myDirectory))
{
if (is_file($entryName))
{
$extensions = explode(".", $entryName);
if(!in_array($extensions[1],$forbiddenExtensions))
{
$dirArray[$i++] = $entryName;
}
}
}
// This function separates the extension from the rest of the file name and returns it
function findExtensions ($filename)
{
$filename = strtolower($filename) ;
$extensions = explode(".", $filename) ; //$extensions = split("[/\\.]", $filename) ;
$n = count($extensions)-1;
$extensions = $extensions[$n];
return $extensions;
}
// Closes $directory
closedir($myDirectory);
// Counts elements in array
$indexCount = count($dirArray);
Print ("$indexCount files<br>\n"); //Display how many files were counted
// Sorts the Array of files
sort($dirArray);
// Prints the Array of files
print("<table width='100%' cellspacing='10'>
<tr>
<td class='database_head'>Filename</td>
<td class='database_head'>Filetype</td>
<td class='database_head'>Filesize</td>
</tr>\n");
// Loops through the array of files and display them all
for($index=0; $index < $indexCount; $index++)
{
// Don't list hidden files
if (substr($dirArray[$index], 0, 1) != ".")
{
print("<tr><td><a href=\"$dirArray[$index]\">$dirArray[$index]</a></td>");
print("<td>");
print(findExtensions($dirArray[$index]));
print("</td>");
print("<td>");
print(filesize($directory.$dirArray[$index]));
print("</td>");
print("</tr>\n");
}
}
print("</table>\n");
?>