如何列出目录中的所有文件。我只想要根目录中的文件。如果根目录中有任何目录,我想跳过这些目录和文件。
现在我正在使用此代码
$folderPath = file_directory_path().'/lexalytics/';
if ($handle = opendir($folderPath)) {
$result .= '<div><ul>';
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
$result .= "<li><a href=../".$folderPath.$entry.">".$entry."</a>\n</li>";
}
}
$result .= '</ul></div>';
closedir($handle);
}
但是它列出了子目录和文件。 怎么能避免那些?请帮助我
答案 0 :(得分:3)
请使用PHP5s新DirectoryIterator课程:
这只会列出文件并排除文件夹:
$directory = file_directory_path().'/lexalytics/';
$filenames = array();
$iterator = new DirectoryIterator($directory);
foreach ($iterator as $fileinfo) {
if ($fileinfo->isFile()) {
$filenames[$fileinfo->getMTime()] = $fileinfo->getFilename();
}
}
答案 1 :(得分:1)
$path = "your-path"; // Open the folder $dir_handle = @opendir($path) or die("Unable to open $path"); // Loop through the files while ($file = readdir($dir_handle)) { if($file == "." || $file == ".." || $file == "index.php" ) continue; echo "<a href=\"$file\">$file</a><br />"; } // Close closedir($dir_handle);
答案 2 :(得分:0)
试试此代码
$folderPath = file_directory_path().'/lexalytics/';
$handle = @opendir($folderPath) or die("Unable to open $path");
$result .= '<div><ul>';
// Loop through the files
while ($entry = @readdir($handle)) {
if(is_file($folderPath.$entry)) {
$result .= "<li><a href=../".$folderPath.$entry.">".$entry."</a>\n</li>";
}
}
$result .= '</ul></div>';
closedir($handle);