之前我写了一段代码,通过这段代码从一个文件夹中获取所有图像。在此版本中,“image_folder”来自用户。当用户单击一个按钮时,它会获得相同的名称和相同的文件夹,并找到与之相关的所有图像。
<?php
if (isset($_GET['grand'])) {
$grand = $_GET['grand'];
$image_folder = "images/products/categories/".$grand;
$desc = $grand;
if (isset($_GET['father'])){
$father = $_GET['father'];
$image_folder = "images/products/categories/".$grand."/".$father;
$desc = $father;
if (isset($_GET['child'])){
$child = $_GET['child'];
$image_folder = "images/products/categories/".$grand."/".$father."/".$child;
$desc = $child;
}
}
/* Show category name */ ?>
<h1><?php echo $desc; ?></h1>
<?php
$all_files = glob($image_folder."/*.*");
for ($i=0; $i<count($all_files); $i++){
$image_url = $all_files[$i];
//Get image name
$image_name = pathinfo($image_url, PATHINFO_FILENAME);
//Get image name
$supported_format = array('gif','jpg','jpeg','png');
$ext = strtolower(pathinfo($image_url, PATHINFO_EXTENSION));
if (in_array($ext, $supported_format)){ ?>
<img src="<?php echo $image_url; ?>">
<?php } else { continue; }//Continue scan ?>
<?php }//End ?>
<?php } else { ?>
<h2>Please Choose Category</h2>
<?php } ?>
这次我只是有一个更复杂的版本,我有点卡住了:) 我想列出文件夹中的每张图片。
这意味着我有一堆产品和一堆图片给他们。这次用户不会点击,我只想将它们全部排序为图像列表。 1001 是产品名称,01-02 是颜色名称,最后一部分 1-2-3-4 只是图像名称。我正在尝试制作目录 :)
任何想法都会对我有很大帮助。谢谢大家<3
要使用 PHP 显示各个文件夹中的所有图像,glob() 函数可以帮助从文件夹及其子文件夹中检索图像文件列表。获得列表后,您可以使用 HTML
<img>
标签。
这是一个示例,您可以列出 Products 文件夹和子文件夹中的所有图像:
<?php
$base_folder = 'Products';
// Get a list of all folders in the base folder
$folders = glob($base_folder . '/*', GLOB_ONLYDIR);
// Iterate over each folder
foreach ($folders as $folder) {
// Get a list of all the image files in the folder
$images = glob($folder . '/*.jpg');
// Iterate over each image
foreach ($images as $image) {
// Get the image name and URL
$image_name = basename($image);
$image_url = $image;
// Display the image using the HTML <img> tag
echo '<img src="' . $image_url . '" alt="' . $image_name . '">';
}
}
?>
您可以修改它以包含其他图像格式或在必要时按文件名模式过滤结果。
如果我对你有帮助,请留下赞成票。或者问我任何问题,我会尽力回答。