php“foreach”扫描图像目录文件夹,检索大的和拇指(用于图像库)
Head正在冒烟,我尝试了几种解决方案,将子目录放入数组中,如php“foreach”扫描图像目录文件夹,检索大的和拇指(用于图像库),php,foreach,get,scandir,Php,Foreach,Get,Scandir,Head正在冒烟,我尝试了几种解决方案,将子目录放入数组中,如 $myDir = '/images/'; // foreach ( $myDirArray = array_filter(glob($myDir . '*'), 'is_dir') as $DirName ) { etc.. 我使用php脚本从特定文件夹中获取大图像,从thumbs文件夹中检索它们的拇指。如果不存在函数make_thumb,脚本将自动生成thumb 对于单个图像文件夹来说,它就像一个符咒,但是需要您的帮助才能在P
$myDir = '/images/'; //
foreach ( $myDirArray = array_filter(glob($myDir . '*'), 'is_dir') as $DirName ) {
etc..
我使用php脚本从特定文件夹中获取大图像,从thumbs文件夹中检索它们的拇指。如果不存在函数make_thumb,脚本将自动生成thumb
对于单个图像文件夹来说,它就像一个符咒,但是需要您的帮助才能在PHP中找到一个通用的解决方案,扫描父目录和其中的文件夹,并保留make_thumb函数
我的代码:
设置image/thumb目录
$images_dir = 'Nature/';
$thumbs_dir = 'Nature/thumbs/';
$thumbs_size = 120;
获取文件扩展名:
function get_file_extension($file_name) {
return substr(strrchr($file_name,'.'),1);
}
获取文件
function get_files($images_dir,$exts = array('jpg')) {
$files = array();
if($handle = opendir($images_dir)) {
while(false !== ($file = readdir($handle))) {
$extension = strtolower(get_file_extension($file));
if($extension && in_array($extension,$exts)) {
$files[] = $file;
}
}
closedir($handle);
}
return $files;
}
生成图库并提取标题以供进一步使用
$image_files = get_files($images_dir);
if(count($image_files)) {
$index = 0;
foreach($image_files as $index=>$file) {
$index++;
$thumbnail_image = $thumbs_dir.$file;
$filename = $file;
$caption = preg_replace(array('/^.+--/', '/\.(jpg|jpeg|gif|png|bmp)$/', '/_/'), array('','',' '), $filename); //Extracting the caption
if(!file_exists($thumbnail_image)) {
$extension = get_file_extension($thumbnail_image);
if($extension) {
make_thumb($images_dir.$file,$thumbnail_image,$thumbs_size);
}
}
echo '<a href="',$images_dir.$file,'" title="',$caption,'"><img src="',$thumbnail_image,'" /></a>';
}
}
else {
echo '<p>There are no images in this gallery.</p>';
}
那么,当您在问题的开头尝试使用foreach循环时发生了什么?您正在记录错误吗?同样值得注意的是,PHP有一些更现代的方法来处理列出的文件系统:我的问题是,我无法找到一种正确的方法来循环遍历文件夹以绘制文件$images\u files=get\u files。