php.ini指定扩展目录,PHP目录中特定文件的列表

PHP目录中特定文件的列表

以下代码将列出目录中的所有文件

if ($handle = opendir('.')) {

while (false !== ($file = readdir($handle)))

{

if (($file != ".")

&& ($file != ".."))

{

$thelist .= '

'.$file.'';

}

}

closedir($handle);

}

?>

List of files:

=$thelist?>

尽管这是非常简单的代码,但可以完成工作。

我现在正在寻找一种仅列出结尾处带有.xml(或.XML)的文件的方法,该怎么办?

9个解决方案

193 votes

您将要使用glob()

例:

$files = glob('/path/to/dir/*.xml');

David Yell answered 2020-06-25T14:24:18Z

48 votes

if ($handle = opendir('.')) {

while (false !== ($file = readdir($handle)))

{

if ($file != "." && $file != ".." && strtolower(substr($file, strrpos($file, '.') + 1)) == 'xml')

{

$thelist .= '

'.$file.'';

}

}

closedir($handle);

}

使用substr和strrpos查看扩展的简单方法

Bob Fincheimer answered 2020-06-25T14:23:53Z

10 votes

$it = new RegexIterator(new DirectoryIterator("."), "/\\.xml\$/i"));

foreach ($it as $filename) {

//...

}

您还可以使用迭代器的递归变体遍历整个目录层次结构。

Artefacto answered 2020-06-25T14:24:38Z

4 votes

我使用以下代码:

{

//foreach (glob("images/*.jpg") as $large)

foreach (glob("*.xml") as $filename) {

//echo "$filename\n";

//echo str_replace("","","$filename\n");

echo str_replace("","","$filename\n");

}

}

?>

Zbigniew answered 2020-06-25T14:24:58Z

2 votes

目录中的列表文件和文件夹(完整代码):

ps 如果只需要特定扩展名,则不必取消注释第5行

# 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 "

File name File SizeLast Modified

for ($i=0;$i

if (!$do_link) {

$line = "

" .

$new_array[$i][0] .

"

" .

number_format(($new_array[$i][1]/1024)) .

"k";

$line = $line . "

" . $new_array[$i][3] . "";

}else{

$line = '

' .

$new_array[$i][0] .

"

";

$line = $line . number_format(($new_array[$i][1]/1024)) .

"k" . "

" .

$new_array[$i][3] . "

";

}

echo $line;

}

echo "

";

?>

T.Todua answered 2020-06-25T14:25:23Z

1 votes

您应该使用glob。

glob('*.xml')

有关使用glob和高级过滤的更多信息:

[HTTP://Dom exception.blogspot.FI/2013/08/PHP-using-functional-programming-佛如.HTML]

Otto-Ville Lamminpää answered 2020-06-25T14:25:54Z

0 votes

最简单的答案是提出另一个条件glob。

但我也建议改用glob。

instanceof me answered 2020-06-25T14:26:18Z

0 votes

您可以像这样扩展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);

}

现在,您可以使用path实例化RecursiveDirectoryIterator,例如:

$iterator = new RecursiveDirectoryIterator('\path\to\dir');

$iterator = new ExtensionFilter($iterator, 'xml, php, ini');

foreach($iterator as $file)

{

echo $file . '
';

}

这将仅列出当前文件夹下的文件。

要同时获取子目录中的文件,将$ iterator(ExtensionFIlter Iterator)作为参数传递给RecursiveIteratorIterator:

$iterator = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);

现在,在此迭代器上运行foreach循环。 您将获得具有指定扩展名的文件

注意:-还请确保在RecursiveIteratorIterator之前运行ExtensionFilter,否则将获取所有文件

draGon answered 2020-06-25T14:27:01Z

0 votes

$ 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 '

$files:-';

print_r($files);

echo '

';

Fasil Palanthodi answered 2020-06-25T14:27:21Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值