php 如何使用PHP从文件夹中读取文件列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/720751/
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
How to read a list of files from a folder using PHP?
提问by Arun Annamalai
I want to read a list the names of files in a folder in a web page using php. is there any simple script to acheive it?
我想使用 php 读取网页中文件夹中文件的名称列表。有没有简单的脚本来实现它?
回答by ólafur Waage
The simplest and most fun way (imo) is glob
最简单最有趣的方式(imo)就是glob
foreach (glob("*.*") as $filename) {
echo $filename."<br />";
}
But the standard way is to use the directory functions.
但标准的方法是使用目录函数。
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
echo "filename: .".$file."<br />";
}
closedir($dh);
}
}
There are also the SPL DirectoryIterator methods. If you are interested
还有SPL DirectoryIterator 方法。如果你感兴趣
回答by Bruno Calza
This is what I like to do:
这是我喜欢做的事情:
$files = array_values(array_filter(scandir($path), function($file) use ($path) {
return !is_dir($path . '/' . $file);
}));
foreach($files as $file){
echo $file;
}
回答by bar
There is this function scandir():
有这个函数scandir():
$dir = 'dir';
$files = scandir($dir, 0);
for($i = 2; $i < count($files); $i++)
print $files[$i]."<br>";
回答by MontDeska
If you have problems with accessing to the path, maybe you need to put this:
如果您在访问路径时遇到问题,也许您需要这样写:
$root = $_SERVER['DOCUMENT_ROOT'];
$path = "/cv/";
// Open the folder
$dir_handle = @opendir($root . $path) or die("Unable to open $path");
回答by Mathias
There is a glob. In this webpage there are good article how to list files in very simple way:
有一个球体。在这个网页中有很好的文章如何以非常简单的方式列出文件:
回答by Rishabh
Check in many folders :
检查许多文件夹:
Folder_1 and folder_2 are name of folders, from which we have to select files.
Folder_1 和 folder_2 是文件夹的名称,我们必须从中选择文件。
$format is required format.
$format 是必需的格式。
<?php
$arr = array("folder_1","folder_2");
$format = ".csv";
for($x=0;$x<count($arr);$x++){
$mm = $arr[$x];
foreach (glob("$mm/*$format") as $filename) {
echo "$filename size " . filesize($filename) . "<br>";
}
}
?>
回答by Vlad Miller
You can use standard directory functions
您可以使用标准目录功能
$dir = opendir('/tmp');
while ($file = readdir($dir)) {
if ($file == '.' || $file == '..') {
continue;
}
echo $file;
}
closedir($dir);
回答by jim_kastrin
There is also a really simple way to do this with the help of the RecursiveTreeIteratorclass, answered here: https://stackoverflow.com/a/37548504/2032235
在RecursiveTreeIterator课程的帮助下,还有一种非常简单的方法可以做到这一点,在这里回答:https: //stackoverflow.com/a/37548504/2032235
回答by alex james
<html>
<head>
<title>Names</title>
</head>
<body style="background-color:powderblue;">
<form method='post' action='alex.php'>
<input type='text' name='name'>
<input type='submit' value='name'>
</form>
Enter Name:
<?php
if($_POST)
{
$Name = $_POST['name'];
$count = 0;
$fh=fopen("alex.txt",'a+') or die("failed to create");
while(!feof($fh))
{
$line = chop(fgets($fh));
if($line==$Name && $line!="")
$count=1;
}
if($count==0 && $Name!="")
{
fwrite($fh, "\r\n$Name");
}
else if($count!=0 && $line!="")
{
echo '<font color="red">'.$Name.', the name you entered is already in the list.</font><br><br>';
}
$count=0;
fseek($fh, 0);
while(!feof($fh))
{
$a = chop(fgets($fh));
echo $a.'<br>';
$count++;
}
if($count<=1)
echo '<br>There are no names in the list<br>';
fclose($fh);
}
?>
</body>
</html>

