我在学习php的过程中如果这是一个愚蠢的问题,请原谅我。我正在尝试创建一个通过文件夹(./ JIN / snoii /)查看的动态图库,并为所述文件夹中的项目数生成html代码。我写的这段代码在文件有图像(当前有一百多张)时不会返回任何内容,有人可以告诉我在哪里搞砸了或指向正确的方向。提前致谢
$CurrentViewFile = "snoii";
function showGallery(){
global $CurrentViewFile;
$galleryHTML = "<h1>Dynamic gallery</h1>";
$galleryHTML .= "<ul>";
$folderToview = "JIN/";
$folderToview .= $CurrentViewFile;
$images = new DirectoryIterator($folderToview);
while($images->valid()){
$galleryHTML .= "<li>!!</li>";
$images->next();
}
$galleryHTML .= "</ul>";
return $galleryHTML;
}
return showGallery();
答案 0 :(得分:0)
您的DirectoryIterator
出现问题,
请尝试这样
$CurrentViewFile = "/snoii";
function showGallery(){
global $CurrentViewFile;
$galleryHTML = "<h1>Dynamic gallery</h1>";
$galleryHTML .= "<ul>";
$folderToview = "JIN/";
$CurrentViewFile .= $folderToview;
foreach (new DirectoryIterator($CurrentViewFile) as $images) {
if($images->valid())
$galleryHTML .= "<li>!!</li>";
$images->next();
}
$galleryHTML .= "</ul>";
return $galleryHTML;
}
$output_data = showGallery();
echo $output_data;
如果你想从迭代器中获取文件名,你应该这样使用,
$images->getFilename()