获取目录中图像的文件名 [英] Get filenames of images in a directory
本文介绍了获取目录中图像的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
应该怎么做才能使用PHP从文件夹/目录中获取图像标题(例如abc.jpg)并将其存储在数组中.
What should be done to get titles (eg abc.jpg) of images from a folder/directory using PHP and storing them in an array.
例如:
a[0] = 'ac.jpg'
a[1] = 'zxy.gif'
等
我将在幻灯片放映中使用该阵列.
I will be using the array in a slide show.
推荐答案
当然有可能.查看 opendir的文档,然后将每个文件推送到结果数组.如果您使用的是PHP5,请查看 DirectoryIterator .这是遍历目录内容的一种更流畅,更干净的方法!
It's certainly possible. Have a look at the documentation for opendir and push every file to a result array. If you're using PHP5, have a look at DirectoryIterator. It is a much smoother and cleaner way to traverse the contents of a directory!
编辑:建立在 opendir 上:
$dir = "/etc/php5/";
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
$images = array();
while (($file = readdir($dh)) !== false) {
if (!is_dir($dir.$file)) {
$images[] = $file;
}
}
closedir($dh);
print_r($images);
}
}
这篇关于获取目录中图像的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文