php PHP如何一一获取文件夹中的图片并显示在页面中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1215799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the pictures from folder one by one and display in the page using PHP
提问by Rajasekar
How to fetch the images from a folder and display it in the page, whether i can resize it in php itself or i have to resize it and upload it separtely to display it as thumbnails?
如何从文件夹中获取图像并将其显示在页面中,我是否可以在 php 中调整它的大小,或者我必须调整它的大小并单独上传以将其显示为缩略图?
回答by Imran
Here's the basic structure for traversing a directory and doing something with the image files (given 'images'is a directory in the same directory of your script)
这是遍历目录并对图像文件执行某些操作的基本结构(假设'images'是与脚本相同的目录中的目录)
$image_types = array(
'gif' => 'image/gif',
'png' => 'image/png',
'jpg' => 'image/jpeg',
);
foreach (scandir('images') as $entry) {
if (!is_dir($entry)) {
if (in_array(mime_content_type('images/'. $entry), $image_types)) {
// do something with image
}
}
}
From here, you can send the images directly to browser, generate tags for HTML page or create thumbnails with GD functionsand store them for displaying.
从这里,您可以将图像直接发送到浏览器,为 HTML 页面生成标签或使用GD 功能创建缩略图并存储它们以供显示。
回答by Shiva Srikanth Thummidi
i think this may help you!
我想这可能对你有帮助!
<?
$string =array();
$filePath='directorypath/';
$dir = opendir($filePath);
while ($file = readdir($dir)) {
if (eregi("\.png",$file) || eregi("\.jpg",$file) || eregi("\.gif",$file) ) {
$string[] = $file;
}
}
while (sizeof($string) != 0){
$img = array_pop($string);
echo "<img src='$filePath$img' width='100px'/>";
}
?>
回答by user2477139
eregiis deprecated now so you can use preg_matchinstead
eregi现在已弃用,因此您可以preg_match改用
<?php
$string =array();
$filePath='directorypath/';
$dir = opendir($filePath);
while ($file = readdir($dir)) {
if (preg_match("/.png/",$file) || preg_match("/.jpg/",$file) || preg_match("/.gif/",$file) ) {
$string[] = $file;
}
}
while (sizeof($string) != 0){
$img = array_pop($string);
echo "<img src='$filePath$img' >";
}
?>
回答by Emile Bergeron
Here's a one-liner based on another answerto a similar question:
这是基于对类似问题的另一个答案的单行:
// this will get you full path to images file.
$data = glob("path/to/images/*.{jpg,gif,png,bmp}", GLOB_BRACE);
// this will get you only the filenames
$data= array_map('basename', $data);
Originally, I wanted to use @Imran solution, but mime_content_typewasn't available and the server (on which I have zero control) uses old versions of Apache and Php.
最初,我想使用@Imran 解决方案,但mime_content_type不可用,并且服务器(我对其进行零控制)使用旧版本的 Apache 和 Php。
So I changed it a little to work with file extension instead and I'm giving it here.
所以我稍微改变了它以使用文件扩展名,我在这里提供它。
$imgDir = "images_dir";
// make sure it's a directory
if (file_exists($imgDir)) {
// select the extensions you want to take into account
$image_ext = array(
'gif',
'png',
'jpg',
'jpeg'
);
foreach (scandir($imgDir) as $entry) {
if (! is_dir($entry)) { // no need to weed out '.' and '..'
if (in_array(
strtolower(pathinfo($entry, PATHINFO_EXTENSION)),
$image_ext)) {
// do something with the image file.
}
}
}
}
The code is tested and is working.
代码已经过测试并且正在运行。
回答by redShadow
Have a look at:
看一下:
To resize images directly from PHP:
要直接从 PHP 调整图像大小:

