我有下面的 PHP 代码,我想在一些帮助下对其进行一些修改。
我需要根据指向特定文件夹的 URL(使用表达式引擎)生成图像列表。
目前这段代码工作得很好,但我需要它做另外两件事...
-
如果找不到文件夹,则向上移动文件夹...
例如,如果它找不到目录/子目录,它将关闭并在目录中搜索。(这显然需要在某个级别停止)
-
生成文件夹中所有图像的列表,而不仅仅是像下面这样的图像.
<前><代码>isFile() && !preg_match('/-c\.jpg$/', $fileinfo->getFilename())) {
$bgimagearray[] = "'" 。 $fileinfo->getFilename() 。 “'”;
}
}
$bgimage = array_rand($bgimagearray);
?>
" alt="{last_segment}" //>
I have this PHP code below that i would like to adapt a little with some help.
I need to produce a list of images based on the URL pointing to a specific folder (using expression engine).
Currently this code works really well but i need it to do two more things...
-
To move up a folder if it doesn't find one...
e.g if it doesn't find directory/subdirectory it will go off and search in directory instead.(this would need to stop obviously at a certain level)
-
Produce a list of all images in the folder, not just one like below.
<?php
$bgimagearray = array();
$iterator = new DirectoryIterator("sites/domain.co.uk/public_html/assets/images/bg-images/{last_segment}");
foreach ($iterator as $fileinfo) {
if ($fileinfo->isFile() && !preg_match('/-c\.jpg$/', $fileinfo->getFilename())) {
$bgimagearray[] = "'" . $fileinfo->getFilename() . "'";
}
}
$bgimage = array_rand($bgimagearray);
?>
<div id="backgroundImage">
<img src="{site_url}assets/images/bg-images/{last_segment}/<?php echo trim($bgimagearray[$bgimage], "'"); ?>" alt="{last_segment}" />
</div>
发布评论
评论(1)
对于第一个问题,您可以使用dirname php function。对于另一个问题,即获取所有文件而不是一个文件,您已经在
$bgimagearray
中拥有整个数组,因此您可以使用 foreach。On the first issue, you can use dirname php function. For the other issue, the one of getting all the files instead of one, you already have the whole array in
$bgimagearray
, so you can iterate it with a foreach.