PHP:获取我的图像目录中包含的所有文件名的列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2059891/
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
PHP: Get list of all filenames contained within my images directory
提问by lewisqic
I have been trying to figure out a way to list all files contained within a directory. I'm not quite good enough with php to solve it on my own so hopefully someone here can help me out.
我一直试图找出一种方法来列出目录中包含的所有文件。我对 php 不够好,无法自己解决,所以希望这里有人可以帮助我。
I need a simple php script that will load all filenames contained within my images directory into an array. Any help would be greatly appreciated, thanks!
我需要一个简单的 php 脚本,它将把我的图像目录中包含的所有文件名加载到一个数组中。任何帮助将不胜感激,谢谢!
回答by a.yastreb
回答by Sampson
回答by Gordon
Either scandir()as suggested elsewhere or
无论是scandir()作为其他地方的建议或
glob()— Find pathnames matching a pattern
glob()— 查找匹配模式的路径名
Example
例子
$images = glob("./images/*.gif");
print_r($images);
/* outputs
Array (
[0] => 'an-image.gif'
[1] => 'another-image.gif'
)
*/
Or, to walk over the files in directory directly instead of getting an array, use
或者,要直接遍历目录中的文件而不是获取数组,请使用
Example
例子
foreach (new DirectoryIterator('.') as $item) {
echo $item, PHP_EOL;
}
To go into subdirectories as well, use RecursiveDirectoryIterator:
要进入子目录,请使用RecursiveDirectoryIterator:
$items = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator('.'),
RecursiveIteratorIterator::SELF_FIRST
);
foreach($items as $item) {
echo $item, PHP_EOL;
}
To list just the filenames (w\out directories), remove RecursiveIteratorIterator::SELF_FIRST
要仅列出文件名(w\out 目录),请删除 RecursiveIteratorIterator::SELF_FIRST
回答by Anthony
You can also use the Standard PHP Library's DirectoryIteratorclass, specifically the getFilenamemethod:
您还可以使用标准 PHP 库的DirectoryIterator类,特别是getFilename方法:
$dir = new DirectoryIterator("/path/to/images");
foreach ($dir as $fileinfo) {
echo $fileinfo->getFilename() . "\n";
}
回答by Hardik
This will gives you all the files in links.
这将为您提供链接中的所有文件。
<?php
$path = $_SERVER['DOCUMENT_ROOT']."/your_folder/";
$files = scandir($path);
$count=1;
foreach ($files as $filename)
{
if($filename=="." || $filename==".." || $filename=="download.php" || $filename=="index.php")
{
//this will not display specified files
}
else
{
echo "<label >".$count.". </label>";
echo "<a href="download.php/?filename=".$filename."">".$filename."</a>
";
$count++;
}
}
?>
回答by freeji
Maybe this function can be useful in the future. You can manipulate the function if you need to echo things or want to do other stuff.
也许这个功能在未来会有用。如果您需要回显或想做其他事情,您可以操作该函数。
$wavs = array();
$wavs = getAllFiles('folder_name',$wavs,'wav');
$allTypesOfFiles = array();
$wavs = getAllFiles('folder_name',$allTypesOfFiles);
//explanation of arguments from the getAllFiles() function
//$dir -> folder/directory you want to get all the files from.
//$allFiles -> to store all the files in and return in the and.
//$extension -> use this argument if you want to find specific files only, else keept empty to find all type of files.
function getAllFiles($dir,$allFiles,$extension = null){
$files = scandir($dir);
foreach($files as $file){
if(is_dir($dir.'/'.$file)) {
$allFiles = getAllFiles($dir.'/'.$file,$allFiles,$extension);
}else{
if(empty($extension) || $extension == pathinfo($dir.'/'.$file)['extension']){
array_push($allFiles,$dir.'/'.$file);
}
}
}
return $allFiles;
}

