[英]PHP find a specific file in directories listed
我正在创建一个文章系统,我需要将所有文章include
在目录中列出的目录中。 例:
由于我是PHP新手,所以我不知道该怎么做。 所有帮助表示赞赏!
这是为您提供的解决方案:
$base_dir = './articles';
$filename = 'article.php';
// Listing all directories in $base_dir
$directories = scandir($base_dir);
// Looping over the directories
foreach($directories as $directory) {
if (! in_array($directory, array('.', '..'))) {
$filepath = $base_dir.'/'.$directory.'/'.$filename;
// if the file exists, we include it
if (is_file($filepath)) {
include_once($filepath);
}
}
}
快又脏...
$data = '';
$dir = "path_to_your_articles";
$dirHandle = opendir($dir);
$lookForThis = 'article.php';
while ($file = readdir($dirHandle)) {
if(!is_dir($file)){
if($file == $lookForThis){
$data .= $file;
}
}
}
closedir($dirHandle);
echo $data;
希望这是有用的...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.