The following code will list all the file in a directory
以下代码将列出目录中的所有文件
<?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>
While this is very simple code it does the job.
虽然这是非常简单的代码,但它完成了这项工作。
I'm now looking for a way to list ONLY files that have .xml (or .XML) at the end, how do I do that?
我现在正在寻找一种方法来列出最后只有.xml(或.XML)的文件,我该怎么做?
8 个解决方案
#1
45
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);
}
A simple way to look at the extension using substr and strrpos
使用substr和strrpos查看扩展的简单方法
#2
#3
9
$it = new RegexIterator(new DirectoryIterator("."), "/\\.xml\$/i"));
foreach ($it as $filename) {
//...
}
You can also use the recursive variants of the iterators to traverse an entire directory hierarchy.
您还可以使用迭代器的递归变体遍历整个目录层次结构。
#4
2
LIST FILES and FOLDERS in a directory (Full Code):
p.s. you have to uncomment the 5th line if you want only for specific extensions
目录中的列表文件和文件夹(完整代码):p.s.如果您只想要特定的扩展,则必须取消注释第5行
<?PHP
# The current directory
$directory = dir("./");
# If you want to turn on Extension Filter, then uncomment this:
### $allowed_ext = array(".sample", ".png", ".jpg", ".jpeg", ".txt", ".doc", ".xls");
## Description of the soft: list_dir_files.php
## Major credits: phpDIRList 2.0 -(c)2005 Ulrich S. Kapp :: Systemberatung ::
$do_link = TRUE;
$sort_what = 0; //0- by name; 1 - by size; 2 - by date
$sort_how = 0; //0 - ASCENDING; 1 - DESCENDING
# # #
function dir_list($dir){
$i=0;
$dl = array();
if ($hd = opendir($dir)) {
while ($sz = readdir($hd)) {
if (preg_match("/^\./",$sz)==0) $dl[] = $sz;$i.=1;
}
closedir($hd);
}
asort($dl);
return $dl;
}
if ($sort_how == 0) {
function compare0($x, $y) {
if ( $x[0] == $y[0] ) return 0;
else if ( $x[0] < $y[0] ) return -1;
else return 1;
}
function compare1($x, $y) {
if ( $x[1] == $y[1] ) return 0;
else if ( $x[1] < $y[1] ) return -1;
else return 1;
}
function compare2($x, $y) {
if ( $x[2] == $y[2] ) return 0;
else if ( $x[2] < $y[2] ) return -1;
else return 1;
}
}else{
function compare0($x, $y) {
if ( $x[0] == $y[0] ) return 0;
else if ( $x[0] < $y[0] ) return 1;
else return -1;
}
function compare1($x, $y) {
if ( $x[1] == $y[1] ) return 0;
else if ( $x[1] < $y[1] ) return 1;
else return -1;
}
function compare2($x, $y) {
if ( $x[2] == $y[2] ) return 0;
else if ( $x[2] < $y[2] ) return 1;
else return -1;
}
}
##################################################
# We get the information here
##################################################
$i = 0;
while($file=$directory->read()) {
$file = strtolower($file);
$ext = strrchr($file, '.');
if (isset($allowed_ext) && (!in_array($ext,$allowed_ext)))
{
// dump
}
else {
$temp_info = stat($file);
$new_array[$i][0] = $file;
$new_array[$i][1] = $temp_info[7];
$new_array[$i][2] = $temp_info[9];
$new_array[$i][3] = date("F d, Y", $new_array[$i][2]);
$i = $i + 1;
}
}
$directory->close();
##################################################
# We sort the information here
#################################################
switch ($sort_what) {
case 0:
usort($new_array, "compare0");
break;
case 1:
usort($new_array, "compare1");
break;
case 2:
usort($new_array, "compare2");
break;
}
###############################################################
# We display the infomation here
###############################################################
$i2 = count($new_array);
$i = 0;
echo "<table border=1>
<tr>
<td width=150> File name</td>
<td width=100> File Size</td>
<td width=100>Last Modified</td>
</tr>";
for ($i=0;$i<$i2;$i++) {
if (!$do_link) {
$line = "<tr><td align=right>" .
$new_array[$i][0] .
"</td><td align=right>" .
number_format(($new_array[$i][1]/1024)) .
"k";
$line = $line . "</td><td align=right>" . $new_array[$i][3] . "</td></tr>";
}else{
$line = '<tr><td align=right><A HREF="' .
$new_array[$i][0] . '">' .
$new_array[$i][0] .
"</A></td><td align=right>";
$line = $line . number_format(($new_array[$i][1]/1024)) .
"k" . "</td><td align=right>" .
$new_array[$i][3] . "</td></tr>";
}
echo $line;
}
echo "</table>";
?>
#5
2
I use this code:
我用这个代码:
<?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");
}
}
?>
#6
1
You should use glob.
你应该使用glob。
glob('*.xml')
More about using glob and advanced filtering:
有关使用glob和高级过滤的更多信息:
http://domexception.blogspot.fi/2013/08/php-using-functional-programming-for.html
http://domexception.blogspot.fi/2013/08/php-using-functional-programming-for.html
#7
0
Simplest answer is to put another condition '.xml' == strtolower(substr($file, -3))
.
最简单的答案是放入另一个条件'.xml'== strtolower(substr($ file,-3))。
But I'd recommend using glob
instead too.
但我也建议使用glob。
#8
0
You can extend the RecursiveFilterIterator class like this:
您可以像这样扩展RecursiveFilterIterator类:
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);
}
Now you can instantiate RecursiveDirectoryIterator with path as an argument like this:
现在你可以用路径作为参数来实例化RecursiveDirectoryIterator,如下所示:
$iterator = new RecursiveDirectoryIterator('\path\to\dir');
$iterator = new ExtensionFilter($iterator, 'xml, php, ini');
foreach($iterator as $file)
{
echo $file . '<br />';
}
This will list files under the current folder only.
To get the files in subdirectories also, pass the $iterator ( ExtensionFIlter Iterator) to RecursiveIteratorIterator as argument:
这将仅列出当前文件夹下的文件。要获取子目录中的文件,请将$ iterator(ExtensionFIlter Iterator)作为参数传递给RecursiveIteratorIterator:
$iterator = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);
Now run the foreach loop on this iterator. You will get the files with specified extension
现在在这个迭代器上运行foreach循环。您将获得具有指定扩展名的文件
Note:-- Also make sure to run the ExtensionFilter before RecursiveIteratorIterator, otherwise you will get all the files
注意: - 还要确保在RecursiveIteratorIterator之前运行ExtensionFilter,否则你将获得所有文件
#1
45
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);
}
A simple way to look at the extension using substr and strrpos
使用substr和strrpos查看扩展的简单方法
#2
#3
9
$it = new RegexIterator(new DirectoryIterator("."), "/\\.xml\$/i"));
foreach ($it as $filename) {
//...
}
You can also use the recursive variants of the iterators to traverse an entire directory hierarchy.
您还可以使用迭代器的递归变体遍历整个目录层次结构。
#4
2
LIST FILES and FOLDERS in a directory (Full Code):
p.s. you have to uncomment the 5th line if you want only for specific extensions
目录中的列表文件和文件夹(完整代码):p.s.如果您只想要特定的扩展,则必须取消注释第5行
<?PHP
# The current directory
$directory = dir("./");
# If you want to turn on Extension Filter, then uncomment this:
### $allowed_ext = array(".sample", ".png", ".jpg", ".jpeg", ".txt", ".doc", ".xls");
## Description of the soft: list_dir_files.php
## Major credits: phpDIRList 2.0 -(c)2005 Ulrich S. Kapp :: Systemberatung ::
$do_link = TRUE;
$sort_what = 0; //0- by name; 1 - by size; 2 - by date
$sort_how = 0; //0 - ASCENDING; 1 - DESCENDING
# # #
function dir_list($dir){
$i=0;
$dl = array();
if ($hd = opendir($dir)) {
while ($sz = readdir($hd)) {
if (preg_match("/^\./",$sz)==0) $dl[] = $sz;$i.=1;
}
closedir($hd);
}
asort($dl);
return $dl;
}
if ($sort_how == 0) {
function compare0($x, $y) {
if ( $x[0] == $y[0] ) return 0;
else if ( $x[0] < $y[0] ) return -1;
else return 1;
}
function compare1($x, $y) {
if ( $x[1] == $y[1] ) return 0;
else if ( $x[1] < $y[1] ) return -1;
else return 1;
}
function compare2($x, $y) {
if ( $x[2] == $y[2] ) return 0;
else if ( $x[2] < $y[2] ) return -1;
else return 1;
}
}else{
function compare0($x, $y) {
if ( $x[0] == $y[0] ) return 0;
else if ( $x[0] < $y[0] ) return 1;
else return -1;
}
function compare1($x, $y) {
if ( $x[1] == $y[1] ) return 0;
else if ( $x[1] < $y[1] ) return 1;
else return -1;
}
function compare2($x, $y) {
if ( $x[2] == $y[2] ) return 0;
else if ( $x[2] < $y[2] ) return 1;
else return -1;
}
}
##################################################
# We get the information here
##################################################
$i = 0;
while($file=$directory->read()) {
$file = strtolower($file);
$ext = strrchr($file, '.');
if (isset($allowed_ext) && (!in_array($ext,$allowed_ext)))
{
// dump
}
else {
$temp_info = stat($file);
$new_array[$i][0] = $file;
$new_array[$i][1] = $temp_info[7];
$new_array[$i][2] = $temp_info[9];
$new_array[$i][3] = date("F d, Y", $new_array[$i][2]);
$i = $i + 1;
}
}
$directory->close();
##################################################
# We sort the information here
#################################################
switch ($sort_what) {
case 0:
usort($new_array, "compare0");
break;
case 1:
usort($new_array, "compare1");
break;
case 2:
usort($new_array, "compare2");
break;
}
###############################################################
# We display the infomation here
###############################################################
$i2 = count($new_array);
$i = 0;
echo "<table border=1>
<tr>
<td width=150> File name</td>
<td width=100> File Size</td>
<td width=100>Last Modified</td>
</tr>";
for ($i=0;$i<$i2;$i++) {
if (!$do_link) {
$line = "<tr><td align=right>" .
$new_array[$i][0] .
"</td><td align=right>" .
number_format(($new_array[$i][1]/1024)) .
"k";
$line = $line . "</td><td align=right>" . $new_array[$i][3] . "</td></tr>";
}else{
$line = '<tr><td align=right><A HREF="' .
$new_array[$i][0] . '">' .
$new_array[$i][0] .
"</A></td><td align=right>";
$line = $line . number_format(($new_array[$i][1]/1024)) .
"k" . "</td><td align=right>" .
$new_array[$i][3] . "</td></tr>";
}
echo $line;
}
echo "</table>";
?>
#5
2
I use this code:
我用这个代码:
<?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");
}
}
?>
#6
1
You should use glob.
你应该使用glob。
glob('*.xml')
More about using glob and advanced filtering:
有关使用glob和高级过滤的更多信息:
http://domexception.blogspot.fi/2013/08/php-using-functional-programming-for.html
http://domexception.blogspot.fi/2013/08/php-using-functional-programming-for.html
#7
0
Simplest answer is to put another condition '.xml' == strtolower(substr($file, -3))
.
最简单的答案是放入另一个条件'.xml'== strtolower(substr($ file,-3))。
But I'd recommend using glob
instead too.
但我也建议使用glob。
#8
0
You can extend the RecursiveFilterIterator class like this:
您可以像这样扩展RecursiveFilterIterator类:
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);
}
Now you can instantiate RecursiveDirectoryIterator with path as an argument like this:
现在你可以用路径作为参数来实例化RecursiveDirectoryIterator,如下所示:
$iterator = new RecursiveDirectoryIterator('\path\to\dir');
$iterator = new ExtensionFilter($iterator, 'xml, php, ini');
foreach($iterator as $file)
{
echo $file . '<br />';
}
This will list files under the current folder only.
To get the files in subdirectories also, pass the $iterator ( ExtensionFIlter Iterator) to RecursiveIteratorIterator as argument:
这将仅列出当前文件夹下的文件。要获取子目录中的文件,请将$ iterator(ExtensionFIlter Iterator)作为参数传递给RecursiveIteratorIterator:
$iterator = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);
Now run the foreach loop on this iterator. You will get the files with specified extension
现在在这个迭代器上运行foreach循环。您将获得具有指定扩展名的文件
Note:-- Also make sure to run the ExtensionFilter before RecursiveIteratorIterator, otherwise you will get all the files
注意: - 还要确保在RecursiveIteratorIterator之前运行ExtensionFilter,否则你将获得所有文件