最好的办法是浏览整个文件夹并相应地过滤图片,如下所示:
$myPicFolder = "path/to/pictures/";
$thePics = array();
if (is_dir($myPicFolder)) {
if ($dh = opendir($myPicFolder)) {
while (($file = readdir($dh)) !== false)) {
if(!is_dir($myPicFolder . $file){
list($intersectionID,$section,$imgID) = explode('_'.$file);//Seperates the parts of the name for easier access
//check if the ID is already a key in $thePics
if(!array_key_exists($intersectionID,$thePics)){
//Add the new ID if it does not exist with an empty array.
$thePics[$intersectionID]=array();
}
if(!array_key_exists($section,$thePics[$intersectionID])){
//Add the new ID if it does not exist with an empty array.
$thePics[$intersectionID][$section]=array();
}
// Add a new part to the ID since each picture ID has 4 pictures.
$thePics[$intersectionID][$section][] = $file;
}
}
closedir($dh);
}
}
//$thePics will then look like:
// array('1234'=>array(
// '1'=>'1234_1_01.jpeg',
// '2'=>'1234_2_03.jpeg',
// '3'=>'1234_3_02.jpeg',
// '4'=>'1234_4_01.jpeg'),
// '345422'=>array(
// '1'=>'345422_1_01.jpeg',
// '2'=>'345422_2_02.jpeg')
// );
//Cycle through the pictures...
foreach($thePics as $pictureID=>$quad){
echo '<div class="intersection">';
foreach($quad as $quadPart=>$pictures){
echo '<div class=”s1”>';
foreach($pictures as $pictureSRC){
echo '<img src="'.$pictureSRC.'"/>';
}
echo '</div>';
}
echo '</div>';
}
请记住对文件性质进行更多验证,以确保您只发送图片......也许测试文件扩展名和 mime 类型......
编辑:添加了一些注释并使用 list() 为我正在使用的变量提供明确的名称,以便更好地理解。