Possible Duplicate:
Get the hierarchy of a directory with PHP
Getting the names of all files in a directory with PHP可能的重复:使用PHP获取目录的层次结构,使用PHP获取目录中所有文件的名称
I have seen functions to list all file in a directory but how can I list all the files in sub-directories too, so it returns an array like?
我已经看到了列出目录中所有文件的函数,但是我如何列出子目录中的所有文件,所以它返回一个数组?
$files = files("foldername");
So $files
is something similar to
$files类似于
array("file.jpg", "blah.word", "name.fileext")
5 个解决方案
#1
96
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator('.')) as $filename)
{
// filter out "." and ".."
if ($filename->isDir()) continue;
echo "$filename\n";
}
PHP documentation:
PHP文档:
- RecursiveDirectoryIterator
- RecursiveDirectoryIterator
- RecursiveIteratorIterator
- RecursiveIteratorIterator
#2
21
So you're looking for a recursive directory listing?
你在寻找递归目录列表吗?
function directoryToArray($directory, $recursive) {
$array_items = array();
if ($handle = opendir($directory)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (is_dir($directory. "/" . $file)) {
if($recursive) {
$array_items = array_merge($array_items, directoryToArray($directory. "/" . $file, $recursive));
}
$file = $directory . "/" . $file;
$array_items[] = preg_replace("/\/\//si", "/", $file);
} else {
$file = $directory . "/" . $file;
$array_items[] = preg_replace("/\/\//si", "/", $file);
}
}
}
closedir($handle);
}
return $array_items;
}
#3
4
I think you're looking for php's glob function. You can call glob(**)
to get a recursive file listing.
我想你是在寻找php的glob函数。您可以调用glob(**)来获得一个递归文件列表。
EDIT: I realized that my glob doesn't work reliably on all systems so I submit this much prettier version of the accepted answer.
编辑:我意识到我的glob不能在所有的系统上都可靠地工作,所以我提交了这个更漂亮的版本。
function rglob($pattern='*', $flags = 0, $path='')
{
$paths=glob($path.'*', GLOB_MARK|GLOB_ONLYDIR|GLOB_NOSORT);
$files=glob($path.$pattern, $flags);
foreach ($paths as $path) { $files=array_merge($files,rglob($pattern, $flags, $path)); }
return $files;
}
from: http://www.phpfreaks.com/forums/index.php?topic=286156.0
来自:http://www.phpfreaks.com/forums/index.php?topic=286156.0
#4
2
function files($path,&$files = array())
{
$dir = opendir($path."/.");
while($item = readdir($dir))
if(is_file($sub = $path."/".$item))
$files[] = $item;else
if($item != "." and $item != "..")
files($sub,$files);
return($files);
}
print_r(files($_SERVER['DOCUMENT_ROOT']));
#5
0
I needed to implement the reading of a given directory, and relying on the function of Chuck Vose, I created this page to read the directories relying on JQuery:
我需要实现对给定目录的读取,依靠Chuck Vose的功能,我创建了这个页面,依靠JQuery读取目录:
<?php
/**
* Recovers folder structure and files of a certain path
*
* @param string $path Folder where files are located
* @param string $pattern Filter by extension
* @param string $flags Flags to be passed to the glob
* @return array Folder structure
*/
function getFolderTree($path)
{
//Recovers files and directories
$paths = glob($path . "*", GLOB_MARK | GLOB_ONLYDIR | GLOB_NOSORT);
$files = glob($path . "*");
//Traverses the directories found
foreach ($paths as $key => $path)
{
//Create directory if exists
$directory = explode("\\", $path);
unset($directory[count($directory) - 1]);
$directories[end($directory)] = getFolderTree($path);
//Verify if exists files
foreach ($files as $file)
{
if (strpos(substr($file, 2), ".") !== false)
$directories[] = substr($file, (strrpos($file, "\\") + 1));
}
}
//Return the directories
if (isset($directories))
{
return $directories;
}
//Returns the last level of folder
else
{
$files2return = Array();
foreach ($files as $key => $file)
$files2return[] = substr($file, (strrpos($file, "\\") + 1));
return $files2return;
}
}
/**
* Creates the HTML for the tree
*
* @param array $directory Array containing the folder structure
* @return string HTML
*/
function createTree($directory)
{
$html = "<ul>";
foreach($directory as $keyDirectory => $eachDirectory)
{
if(is_array($eachDirectory))
{
$html .= "<li class='closed'><span class='folder'>" . $keyDirectory . "</span>";
$html .= createTree($eachDirectory);
$html .= "</li>";
}
else
{
$html .= "<li><span class='file'>" . $eachDirectory . "</span></li>";
}
}
$html .= "</ul>";
return $html;
}
//Create output
$directory = getFolderTree('..\videos');
$htmlTree = createTree($directory["videos"]);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1"/>
<title>PHP Directories</title>
<link rel="stylesheet" href="http://jquery.bassistance.de/treeview/jquery.treeview.css" />
<link rel="stylesheet" href="http://jquery.bassistance.de/treeview/demo/screen.css" />
<script src="http://jquery.bassistance.de/treeview/lib/jquery.js" type="text/javascript"></script>
<script src="http://jquery.bassistance.de/treeview/lib/jquery.cookie.js" type="text/javascript"></script>
<script src="http://jquery.bassistance.de/treeview/jquery.treeview.js" type="text/javascript"></script>
<script type="text/javascript" src="http://jquery.bassistance.de/treeview/demo/demo.js"></script>
</head>
<body>
<div id="main">
<ul id="browser" class="filetree">
<?php echo $htmlTree;?>
</ul>
</div>
</body>
</html>
The structure used in the tree with JQuery, the site was taken: http://jquery.bassistance.de/treeview/demo/
使用JQuery在树中使用的结构是:http://jquery.bassistance.de/treeview/demo/
I hope it is useful!
我希望它有用!
#1
96
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator('.')) as $filename)
{
// filter out "." and ".."
if ($filename->isDir()) continue;
echo "$filename\n";
}
PHP documentation:
PHP文档:
- RecursiveDirectoryIterator
- RecursiveDirectoryIterator
- RecursiveIteratorIterator
- RecursiveIteratorIterator
#2
21
So you're looking for a recursive directory listing?
你在寻找递归目录列表吗?
function directoryToArray($directory, $recursive) {
$array_items = array();
if ($handle = opendir($directory)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (is_dir($directory. "/" . $file)) {
if($recursive) {
$array_items = array_merge($array_items, directoryToArray($directory. "/" . $file, $recursive));
}
$file = $directory . "/" . $file;
$array_items[] = preg_replace("/\/\//si", "/", $file);
} else {
$file = $directory . "/" . $file;
$array_items[] = preg_replace("/\/\//si", "/", $file);
}
}
}
closedir($handle);
}
return $array_items;
}
#3
4
I think you're looking for php's glob function. You can call glob(**)
to get a recursive file listing.
我想你是在寻找php的glob函数。您可以调用glob(**)来获得一个递归文件列表。
EDIT: I realized that my glob doesn't work reliably on all systems so I submit this much prettier version of the accepted answer.
编辑:我意识到我的glob不能在所有的系统上都可靠地工作,所以我提交了这个更漂亮的版本。
function rglob($pattern='*', $flags = 0, $path='')
{
$paths=glob($path.'*', GLOB_MARK|GLOB_ONLYDIR|GLOB_NOSORT);
$files=glob($path.$pattern, $flags);
foreach ($paths as $path) { $files=array_merge($files,rglob($pattern, $flags, $path)); }
return $files;
}
from: http://www.phpfreaks.com/forums/index.php?topic=286156.0
来自:http://www.phpfreaks.com/forums/index.php?topic=286156.0
#4
2
function files($path,&$files = array())
{
$dir = opendir($path."/.");
while($item = readdir($dir))
if(is_file($sub = $path."/".$item))
$files[] = $item;else
if($item != "." and $item != "..")
files($sub,$files);
return($files);
}
print_r(files($_SERVER['DOCUMENT_ROOT']));
#5
0
I needed to implement the reading of a given directory, and relying on the function of Chuck Vose, I created this page to read the directories relying on JQuery:
我需要实现对给定目录的读取,依靠Chuck Vose的功能,我创建了这个页面,依靠JQuery读取目录:
<?php
/**
* Recovers folder structure and files of a certain path
*
* @param string $path Folder where files are located
* @param string $pattern Filter by extension
* @param string $flags Flags to be passed to the glob
* @return array Folder structure
*/
function getFolderTree($path)
{
//Recovers files and directories
$paths = glob($path . "*", GLOB_MARK | GLOB_ONLYDIR | GLOB_NOSORT);
$files = glob($path . "*");
//Traverses the directories found
foreach ($paths as $key => $path)
{
//Create directory if exists
$directory = explode("\\", $path);
unset($directory[count($directory) - 1]);
$directories[end($directory)] = getFolderTree($path);
//Verify if exists files
foreach ($files as $file)
{
if (strpos(substr($file, 2), ".") !== false)
$directories[] = substr($file, (strrpos($file, "\\") + 1));
}
}
//Return the directories
if (isset($directories))
{
return $directories;
}
//Returns the last level of folder
else
{
$files2return = Array();
foreach ($files as $key => $file)
$files2return[] = substr($file, (strrpos($file, "\\") + 1));
return $files2return;
}
}
/**
* Creates the HTML for the tree
*
* @param array $directory Array containing the folder structure
* @return string HTML
*/
function createTree($directory)
{
$html = "<ul>";
foreach($directory as $keyDirectory => $eachDirectory)
{
if(is_array($eachDirectory))
{
$html .= "<li class='closed'><span class='folder'>" . $keyDirectory . "</span>";
$html .= createTree($eachDirectory);
$html .= "</li>";
}
else
{
$html .= "<li><span class='file'>" . $eachDirectory . "</span></li>";
}
}
$html .= "</ul>";
return $html;
}
//Create output
$directory = getFolderTree('..\videos');
$htmlTree = createTree($directory["videos"]);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1"/>
<title>PHP Directories</title>
<link rel="stylesheet" href="http://jquery.bassistance.de/treeview/jquery.treeview.css" />
<link rel="stylesheet" href="http://jquery.bassistance.de/treeview/demo/screen.css" />
<script src="http://jquery.bassistance.de/treeview/lib/jquery.js" type="text/javascript"></script>
<script src="http://jquery.bassistance.de/treeview/lib/jquery.cookie.js" type="text/javascript"></script>
<script src="http://jquery.bassistance.de/treeview/jquery.treeview.js" type="text/javascript"></script>
<script type="text/javascript" src="http://jquery.bassistance.de/treeview/demo/demo.js"></script>
</head>
<body>
<div id="main">
<ul id="browser" class="filetree">
<?php echo $htmlTree;?>
</ul>
</div>
</body>
</html>
The structure used in the tree with JQuery, the site was taken: http://jquery.bassistance.de/treeview/demo/
使用JQuery在树中使用的结构是:http://jquery.bassistance.de/treeview/demo/
I hope it is useful!
我希望它有用!