目录中特定文件的PHP列表
下面的代码将列出目录中的所有文件目录中特定文件的PHP列表,php,file,list,directory,Php,File,List,Directory,下面的代码将列出目录中的所有文件 <?php if ($handle = opendir('.')) { while (false !== ($file = readdir($handle))) { if (($file != ".") && ($file != "..")) { $thelist .= '<LI><a href="'.$file.'">'.$
<?php
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle)))
{
if (($file != ".")
&& ($file != ".."))
{
$thelist .= '<LI><a href="'.$file.'">'.$file.'</a>';
}
}
closedir($handle);
}
?>
<P>List of files:</p>
<UL>
<P><?=$thelist?></p>
</UL>
文件列表:
虽然这是一个非常简单的代码,但它可以完成这项工作
我现在正在寻找一种方法,只列出结尾有.xml(或.xml)的文件,如何做到这一点?您可能希望使用
例如:
$files = glob('/path/to/dir/*.xml');
您还可以使用迭代器的递归变量遍历整个目录层次结构。最简单的答案是放置另一个条件
'.xml'==strtolower(substr($file,-3))
但是我建议也使用glob
。如果($handle=opendir('.')){
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != ".." && strtolower(substr($file, strrpos($file, '.') + 1)) == 'xml')
{
$thelist .= '<li><a href="'.$file.'">'.$file.'</a></li>';
}
}
closedir($handle);
}
while(false!=($file=readdir($handle)))
{
如果($file!=““&&&$file!=”。“&&strtolower(substr($file,strrpos($file,”)+1))=='xml')
{
$thelist.='';
}
}
closedir($handle);
}
使用substr和strrpos查看扩展名的简单方法列出目录中的文件和文件夹(完整代码):
p、 如果您只想为特定扩展名取消注释第5行,则必须取消注释
您应该使用glob
glob('*.xml')
有关使用全局和高级筛选的详细信息:
我使用以下代码:
<?php
{
//foreach (glob("images/*.jpg") as $large)
foreach (glob("*.xml") as $filename) {
//echo "$filename\n";
//echo str_replace("","","$filename\n");
echo str_replace("","","<a href='$filename'>$filename</a>\n");
}
}
?>
您可以像这样扩展RecursiveFilterEditor类:
class ExtensionFilter extends RecursiveFilterIterator
{
/**
* Hold the extensions pass to the class constructor
*/
protected $extensions;
/**
* ExtensionFilter constructor.
*
* @param RecursiveIterator $iterator
* @param string|array $extensions Extension to filter as an array ['php'] or
* as string with commas in between 'php, exe, ini'
*/
public function __construct(RecursiveIterator $iterator, $extensions)
{
parent::__construct($iterator);
$this->extensions = is_array($extensions) ? $extensions : array_map('trim', explode(',', $extensions));
}
public function accept()
{
if ($this->hasChildren()) {
return true;
}
return $this->current()->isFile() &&
in_array(strtolower($this->current()->getExtension()), $this->extensions);
}
public function getChildren()
{
return new self($this->getInnerIterator()->getChildren(), $this->extensions);
}
现在,您可以使用path作为参数实例化RecursiveDirectoryIterator,如下所示:
$iterator = new RecursiveDirectoryIterator('\path\to\dir');
$iterator = new ExtensionFilter($iterator, 'xml, php, ini');
foreach($iterator as $file)
{
echo $file . '<br />';
}
现在在此迭代器上运行foreach循环。您将获得具有指定扩展名的文件
$directory = 'pathto/directory';
$files = array();
$allowed_ext = array( "xml", "png", "jpg", "jpeg", "txt", "doc", "xls","csv");
// Check if the directory exists or not
if (file_exists($directory) && is_dir($directory)) {
// Get the files in the directory
$scan_contents = scandir($directory);
// Filter out the current (.) and parent (..) directories
$files_array = array_diff($scan_contents, array('.', '..'));
// Get each files of our directory with line break
foreach ($files_array as $file) {
//Get the file path
$file_path = "$directory/$file";
// Get the file extension
$file_ext = strtolower(pathinfo($file_path, PATHINFO_EXTENSION));
if (in_array($file_ext, $allowed_ext) ) {
$files[] = $file_path;
}
}
}
echo '<pre>$files:-';
print_r($files);
echo '</pre>';
注意:--还要确保在递归迭代器之前运行ExtensionFilter,否则您将获得所有文件$files数组将获得指定扩展名所在目录中的所有文件
$directory = 'pathto/directory';
$files = array();
$allowed_ext = array( "xml", "png", "jpg", "jpeg", "txt", "doc", "xls","csv");
// Check if the directory exists or not
if (file_exists($directory) && is_dir($directory)) {
// Get the files in the directory
$scan_contents = scandir($directory);
// Filter out the current (.) and parent (..) directories
$files_array = array_diff($scan_contents, array('.', '..'));
// Get each files of our directory with line break
foreach ($files_array as $file) {
//Get the file path
$file_path = "$directory/$file";
// Get the file extension
$file_ext = strtolower(pathinfo($file_path, PATHINFO_EXTENSION));
if (in_array($file_ext, $allowed_ext) ) {
$files[] = $file_path;
}
}
}
echo '<pre>$files:-';
print_r($files);
echo '</pre>';
$directory='pathto/directory';
$files=array();
$allowed_ext=数组(“xml”、“png”、“jpg”、“jpeg”、“txt”、“doc”、“xls”、“csv”);
//检查目录是否存在
如果(文件存在($directory)&&is_dir($directory)){
//获取目录中的文件
$scan_contents=scandir($directory);
//筛选出当前(.)和父(…)目录
$files_array=array_diff($scan_contents,array('.','..);
//使用换行符获取目录中的每个文件
foreach($files\u数组为$file){
//获取文件路径
$file_path=“$directory/$file”;
//获取文件扩展名
$file_ext=strtolower(路径信息($file_路径,路径信息扩展名));
if(在数组中($file\u ext,$allowed\u ext)){
$files[]=$file\u路径;
}
}
}
回显“$files:-”;
打印(文件);
回声';
您可以在glob()函数和pathinfo()函数之间混合使用,如下所示
下面的代码将显示特定扩展名“pdf”的文件信息
foreach(glob(“*.pdf”)作为$file){
$file_info=pathinfo(getcwd()./'.$file);
echo$file_info['dirname'],“
”;
echo$file_info['basename'],“
”;
echo$file_info['extension'],“
”;
echo$file_info['filename'],“
”;
回声“
”;
}
在PHP>=5.3中,您也可以使用@Gordon的GlobIterator:Link,这对我不起作用,但这里的内容与示例相同:。我不是PHP程序员,我是一个老C程序员。如果我在哪里添加代码?@Jessica你完全用那一行代码替换你的代码。它将返回具有xml扩展名的所有文件路径的数组。您可以使用foreach
来迭代,然后构建您的HTML列表。+1真棒的答案。我不知道glob()
,从现在起肯定会经常使用它。谢谢@杰西卡:这个答案似乎比选择的答案要好。如何更改它?检查$file
是否为
或。
如果您已经尝试查找某个扩展名,则毫无用处,因为这已经排除了这两种情况。此外,出于安全原因,您可能希望删除路径中的任何。
,以便只能访问子目录,而不能访问同级或父目录。只有当你的目录不是静态的时候才考虑这一点。这回答了最初的问题,但是我认为问题应该是“有没有明确的PHP语法可以为你做到这一点?”答案是肯定的,正如你所看到的投票率最高的答案。这是一个“一个解决方案适合所有人”的回答,考虑了所有可能的可能性和要求。对于一些被认为是理所当然的事情来说,这有点冗长。
$iterator = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);
$directory = 'pathto/directory';
$files = array();
$allowed_ext = array( "xml", "png", "jpg", "jpeg", "txt", "doc", "xls","csv");
// Check if the directory exists or not
if (file_exists($directory) && is_dir($directory)) {
// Get the files in the directory
$scan_contents = scandir($directory);
// Filter out the current (.) and parent (..) directories
$files_array = array_diff($scan_contents, array('.', '..'));
// Get each files of our directory with line break
foreach ($files_array as $file) {
//Get the file path
$file_path = "$directory/$file";
// Get the file extension
$file_ext = strtolower(pathinfo($file_path, PATHINFO_EXTENSION));
if (in_array($file_ext, $allowed_ext) ) {
$files[] = $file_path;
}
}
}
echo '<pre>$files:-';
print_r($files);
echo '</pre>';
foreach ( glob("*.pdf") as $file ) {
$file_info = pathinfo( getcwd().'/'.$file );
echo $file_info['dirname'], "<br>";
echo $file_info['basename'], "<br>";
echo $file_info['extension'], "<br>";
echo $file_info['filename'], "<br>";
echo '<hr>';
}