 |
readdir (PHP 3, PHP 4, PHP 5) readdir -- 从目录句柄中读取条目 说明string readdir ( resource dir_handle )
返回目录中下一个文件的文件名。文件名以在文件系统中的排序返回。
范例
例子 1. 列出目录中的所有文件
请留意下面例子中检查 readdir()
返回值的风格。这里明确地测试返回值是否全等于(值和类型都相同――更多信息参见比较运算符)FALSE,否则任何目录项的名称求值为
FALSE 的都会导致循环停止(例如一个目录名为“0”)。
<?php // 注意在 4.0.0-RC2 之前不存在 !== 运算符
if ($handle = opendir('/path/to/files')) { echo "Directory handle: $handle\n"; echo "Files:\n";
/* 这是正确地遍历目录方法 */ while (false !== ($file = readdir($handle))) { echo "$file\n"; }
/* 这是错误地遍历目录的方法 */ while ($file = readdir($handle)) { echo "$file\n"; }
closedir($handle); } ?>
|
|
例子 2. 列出当前目录的所有文件并去掉 . 和 ..
<?php if ($handle = opendir('.')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { echo "$file\n"; } } closedir($handle); } ?>
|
|
Adlez
20-Oct-2006 10:58
Another way to sort files from directories.
<?php
$path = getcwd();
$dh = opendir($path);
print "Directories <br>************************* <br>";
while (($dir = readdir($dh)) !== false) {
if ( $dir != "." && $dir != ".." ){
$file = @fopen("$path/$dir", "r");
if (!$file){
print "<a href='$dir'>$dir</a><br>";
}
}
}
closedir($dh);
$path = getcwd();
$dh = opendir($path);
print "<br>Files <br>************************* <br>";
while (($dir = readdir($dh)) !== false) {
if ( $dir != "." && $dir != ".." ){
$file = @fopen("$path/$dir", "r");
if ($file){
print "<a href='$dir'>$dir</a><br>";
}
}
}
closedir($dh);
?>
suit (at) rebell (dot) at
18-Oct-2006 09:23
better version of the yesterdays script
first it reads all content and divides it into files and directorys (two arrays)
then the arrays will be sorted and an output is given (directorys first, then the files)
<?php
$startdir = "downloads/";
$dir = "$startdir$subdir";
echo "<table border=\"1\">\n";
echo "<tr>\n";
echo "<td><a href=\"directory.php\">UP ..</a></td>";
echo "<td>$subdir</td>";
echo "</tr>\n";
$array_dir = array();
$array_file = array();
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false)
{
$filename = $file;
$filetype = filetype($dir . $file);
if ($filename == ".." OR $filename == ".")
{
echo "";
}
else
{
if ($filetype == "file")
{
$array_file[] = $filename;
}
elseif ($filetype == "dir")
{
$array_dir[] = $filename;
}
}
}
closedir($dh);
}
}
sort($array_dir);
foreach ($array_dir as $item) {
$subdir_n = "$subdir$item/";
echo "<tr>\n";
echo "<td>dir</td>\n";
echo "<td><a href=\"directory.php?subdir=$subdir_n\">$item</a></td>\n";
echo "</tr>\n";
}
sort($array_file);
foreach ($array_file as $item) {
$subdir_n = "$subdir$item/";
echo "<tr>\n";
echo "<td>file</td>\n";
echo "<td>$item</td>\n";
echo "</tr>\n";
}
echo "</table>\n";
?>
11-Oct-2006 03:08
Hi, i created a simple module to sort the contents of a dir
using sort(). In the first part; it get the content of the directory (not sorted) and in the second part; it sort the contents and write line per line the result
Sorry for my poor english;!
<?
$dir = getcwd(); // the best option is getcwd()
$dh = opendir($dir);
while (false !== ($file = readdir($dh))) {
if (is_dir($file) && filesize($file)) {} else { echo "$file\n";}
}
sort($gi); // can be rsort($gi);
while ($cn < $to){
echo ("$gi[$cn]\n");
$cn = $cn +1;
}
?>
Cynagen cynagen[(at)]gmail_dot_com
03-Oct-2006 05:41
I have recently created a function I have called filearray, which makes use of opendir(), readdir(), and closedir(), to create and output a recursive array in which all files and folders underneath the target main folder ( / = default) that you can parse how you want, array_search it or whatever you need to do. Search 1 or more directories automatically recursively with this.
function filearray($start="/") {
$dir=opendir($start);
while (false !== ($found=readdir($dir))) { $getit[]=$found; }
foreach($getit as $num => $item) {
if (is_dir($start.$item) && $item!="." && $item!="..") { $output["{$item}"]=filearray($start.$item."/"); }
if (is_file($start.$item)) { $output["{$item}"]="FILE"; }
}
closedir($dir);
return $output;
}
phpmanual at generic dot depoll dot de
08-Sep-2006 06:36
Guys.. regarding your epic listImages functions:
function listImages($dirname='.') {
return glob($dirname .'*.{jpg,png,jpeg,gif}', GLOB_BRACE);
}
Cheers,
Niels
runt
06-Sep-2006 01:46
With the help of a clan member from the clan I'm in, I created the following script to give you a password protected list of files that allows directory traversing. It will not allow you to view any file not in the path you chose.
<?
define("def_path", 'your path here');
define("def_pass", 'your password here');
$path = def_path;
$pass = "";
foreach ($_GET as $k => $v) {
if ("path"==$k)
$path="$v/";
if ("pass"==$k)
$pass="$v";
}
// make sure they cant get access to other files
if (strpos($path,def_path) != false)
$path=def_path;
if (strpos($path,"..")!=false)
$path=def_path;
if (!is_dir($path))
$path=def_path;
if (rtrim(substr_replace($path, ' ', 2, 1000)) != def_path) // change 2 to be what you need to get your entire path for the comparison
$path=def_path;
function read_dir($pass, $dirToRead) {
$path4 = opendir($dirToRead);
$dirs = array();
$files = array();
while (false !== ($file = readdir($path4))) {
if($file!="." && $file!="..") {
if(is_dir($dirToRead.$file))
$dirs[]=$dirToRead.$file;
else
$files[]=$file;
}
}
closedir($path4);
// get the next level up for the
$upDir = substr( $dirToRead, 0, strrpos( $dirToRead, '/') );
if( strrpos( $dirToRead, '/') + 1 == strlen("$dirToRead"))
$upDir = substr( $upDir, 0, strrpos( $upDir, '/') );
if($upDir==""){$upDir=def_path;}
echo "<pre>";
echo ltrim(substr_replace($dirToRead, ' ', 0, 1))."\n";
echo "<a href=\"?pass=$pass&path=$upDir\">..</a>\n";
if($dirs) {
natcasesort($dirs);
foreach($dirs as $dir)
echo "<a href=\"?pass=$pass&path=$dir\">" . basename($dir) . "</a>\n";
}
echo "\n";
if ($dirToRead != def_path) {
if($files) {
natcasesort($files);
foreach ($files as $file)
echo $file."\n";
echo "\n";
}
}
echo "</pre>";
}
?>
<html><head><title>HMT Fast Download</title></head>
<body bgcolor="#E5E5E5" text="#000000" link="#006699" vlink="#5493B4">
<?
if($pass==def_pass){
if (is_dir($path)) {
read_dir($pass, $path);
}
}else if($pass==""){
echo "<form action=\"\" method=\"get\" name=\"loginfrm\">";
echo "<input name=\"pass\" type=\"text\" value=\"$pass\">";
echo "<input name=\"login\" type=\"submit\" value=\"login\">";
echo "</form>";
}
else{
header('Location:http://hitlersmissingtestie.clanservers.com/forum/');
}
?>
</body>
</html>
rasmus dot neikes at web dot de
03-Sep-2006 05:38
I needed a function to offer me a list of files to select from. I used the codes from Marijan Greguric and Rick as a basis and got the following:
<script type="text/JavaScript">
function pokazi(dir){
x=document.getElementById(dir);
if(x.style.display == "none"){
x.style.display = "";
}else{
x.style.display = "none";
}
}
</script>
<?php
function ls($dir){
$handle = opendir($dir);
for(;(false !== ($readdir = readdir($handle)));){
if($readdir != '.' && $readdir != '..'){
$path = $dir.'/'.$readdir;
if(is_dir($path)) $output[$readdir] = $this->ls($path);
if(is_file($path)) $output[] = $readdir;
}
}
closedir($handle);
return isset($output)?$output:false;
//closedir($handle);
}
function throwtree ($dir, $name, $base='', $id='', $return = '') {
foreach ($dir as $key=>$node) {
$return .= "<dl style=\"border: none; margin:2px;\">";
if (is_array ($node)) {
$return .= "<dt><a href=\"#\" onClick=\"pokazi('$base$key');return false;\">$key</a></dt><dd style=\" margin-left:20px; display:none;\" id=\"$base$key\">";
$nextID = $base."/".$key;
$nextBase = "$base$key";
$this->throwtree ($node, $name, $nextBase, $nextID, &$return);
$return .= "</dd>";
} else {
$return .= "\n<dt><input name=\"$name\" type=\"radio\" value=\"$id/$node\"> $node</dt>\n";
}
$return .= "</dl>";
}
return $return;
}
?>
They are currently in a class, so you might have to edit the $this-> in the recursions.
The first function ls still creates an array as in the original; the throwtree function uses that array and creates nested defintion-lists of the file- and directory-tree. Nodes can still be folded using JavaScript from Marijan.
The function takes an additional parameter $name from which it generates a list of radio buttons; one per file listed. The values are the paths and file names of the files.
The HTML-String validates; as of now, the JavaScript doesn't degrate gracefully, though. I didn't bother to sort to the array before parsing it, either.
I haven't tested it much yet, but so far it looks good.
Use like this:
<?php
$myclass->throwtree ($myclass->ls ('path/to/files'), 'radioValue');
?>
ugo.quaisse at gmail.com
12-Jul-2006 10:48
Upgrade of davistummer's code :
You can now list your folder by date
you need to rename your file with this syntax :
"name_-_YYYY-mm-dd.ext"
<?php
function trierdossier($dirname, $sortby, $sortdir) {
$ext = array("jpg", "png", "jpeg", "gif", "pdf", "doc", "txt", "xls");
$files = array();
if($handle = opendir($dirname)) {
while(false !== ($file = readdir($handle))){
for($i=0;$i<sizeof($ext);$i++){
if(stristr($file, ".".$ext[$i])){ //NOT case sensitive: OK with JpeG, JPG, ecc.
$filesize = filesize($dirname . "/" . $file);
$date3 = explode("_-_",$file);
$date2 = explode(".pdf",$date3[1]);
$date1 = $date2[0];
$date0 = tostamp($date1);
if($filesize){
$files[] = array(
"name" => $file,
"size" => $filesize,
"date" => $date0
);
}
}
}
}
closedir($handle);
}
//obtain an array of columns
foreach ($files as $key => $row) {
$name[$key] = $row['name'];
$size[$key] = $row['size'];
$date[$key] = $row['date'];
}
return array_multisort($$sortby, $sortdir, $files) ? $files : false;
}
//end
?>
<?php
//affichage
$sortby = "date"; // sort-by column; accepted values: name OR width OR height OR size
$path = "../upload/argumentaires/lcp/"; // path to folder
$sortdir = SORT_DESC; // sorting order flags; accepted values: SORT_ASC or SORT_DESC
$files = trierdossier($path, $sortby, $sortdir);
foreach ($files as $file){
echo $file['name'];
echo $file['date'];
echo $file['size'];
}
?>
repley at freemail dot it
23-May-2006 10:27
Upgrade of davidstummer's listImages function: now with order by and sorting direction (like SQL ORDER BY clause).
<?php
//listImages(string path, string sort-by name | width | height | size, int sorting order flags SORT_ASC | SORT_DESC)
function listImages($dirname, $sortby, $sortdir) {
$ext = array("jpg", "png", "jpeg", "gif");
$files = array();
if($handle = opendir($dirname)) {
while(false !== ($file = readdir($handle))){
for($i=0;$i<sizeof($ext);$i++){
if(stristr($file, ".".$ext[$i])){ //NOT case sensitive: OK with JpeG, JPG, ecc.
$imgdata = getimagesize($dirname . "/" . $file);
$filesize = filesize($dirname . "/" . $file);
//Some formats may contain no image or may contain multiple images.
//In these cases, getimagesize() might not be able to properly determine the image size.
//getimagesize() will return zero for width and height in these cases.
if($imgdata[0] && $imgdata[1] && $filesize){
$files[] = array(
"name" => $file,
"width" => $imgdata[0],
"height" => $imgdata[1],
"size" => $filesize
);
}
}
}
}
closedir($handle);
}
//obtain an array of columns
foreach ($files as $key => $row) {
$name[$key] = $row['name'];
$width[$key] = $row['width'];
$height[$key] = $row['height'];
$size[$key] = $row['size'];
}
return array_multisort($$sortby, $sortdir, $files) ? $files : false;
}
//end listImages
//start test
$sortby = "width"; // sort-by column; accepted values: name OR width OR height OR size
$path = "path"; // path to folder
$sortdir = SORT_ASC; // sorting order flags; accepted values: SORT_ASC or SORT_DESC
$files = listImages($path, $sortby, $sortdir);
foreach ($files as $file){
echo 'name = <strong>' . $file['name'] . '</strong> width = <strong>' . $file['width'] . '</strong> height = <strong>' . $file['height'] . '</strong> size = <strong>' . $file['size'] . '</strong><br />';
}
//end test
?>
Repley.
repley at freemail dot it
19-May-2006 07:53
BugFix of davidstummer at yahoo dot co dot uk listImages() function:
<?php
function listImages($dirname=".") {
$ext = array("jpg", "png", "jpeg", "gif");
$files = array();
if($handle = opendir($dirname)) {
while(false !== ($file = readdir($handle)))
for($i=0;$i<sizeof($ext);$i++)
if(stristr($file, ".".$ext[$i])) //NOT case sensitive: OK with JpeG, JPG, ecc.
$files[] = $file;
closedir($handle);
}
return($files);
}
?>
Thanks to all.
Repley
vorozhko at gmail dot com
19-Apr-2006 04:39
Here is my recursive read dir function, what read dir by mask.
[php]
function ls($dir, $mask /*.php$|.txt$*/)
{
static $i = 0;
$files = Array();
$d = opendir($dir);
while ($file = readdir($d))
{
if ($file == '.' || $file == '..' || eregi($mask, $file) ) continue;
if (is_dir($dir.'/'.$file))
{
$files += ls($dir.'/'.$file, $mask);
continue;
}
$files[$i++] = $dir.'/'.$file;
}
return $files;
}
$f = ls('E:/www/mymoney/lsdu', ".php$" /*no spaces*/);
[/php]
phathead17 at yahoo dot com
10-Apr-2006 02:14
moogman,
While using your excellent readdirsort function, php complains that the next element in the $items array could not be found.
Instead of:
if ($items[$last_read_item] == false)
Just use:
if (!isset($items[$last_read_item]) || $items[$last_read_item] == false)
anonimo
29-Mar-2006 08:06
----------------------
To Marijan Greguric
----------------------
In your script there is a little bug with folders with same name but different path :
i think that is correct to use this id
$id="$path$file";
and not
$id="$file";
bye
L
moogman
23-Feb-2006 07:04
Took cart at kolix dot de's wrapper and changed it into a simple readdir wrapper that does the same, but puts directories first and sorts. Hope it's useful :)
function readdirsort($dir) {
/* Funktastic readdir wrapper. Puts directories first and sorts. */
static $not_first_run, $last_read_item=0, $items;
if (!$not_first_run) {
$not_first_run = true;
while (false !== ($file = readdir($dir))) {
if (is_dir("$file"))
$dirs[] = $file;
else
$files[] = $file;
}
natcasesort($dirs);
natcasesort($files);
$items = array_merge($dirs, $files);
}
if ($items[$last_read_item] == false)
return false;
else
return $items[$last_read_item++];
}
imran at imranaziz dot net
19-Feb-2006 05:02
Based on tips above, I made this function that displays all php files in your whole website, like a tree. It also displays which directory they're in.
$dir = $_SERVER['DOCUMENT_ROOT'];
$shell_command = `find $dir -depth -type f -print`;
$files = explode ("\n", $shell_command);
foreach ($files as $value)
{
$fileInfo = pathinfo($value);
$extension = $fileInfo["extension"];
if ($extension == "php")
{
$cur_dir = str_replace($_SERVER['DOCUMENT_ROOT'], "", dirname($value));
if ($cur_dir != $old_dir)
{
echo "<p><b>" . $cur_dir . "</b></p>";
$old_dir = $cur_dir;
}
echo $cur_dir . "/" . basename($value) . "<BR>";
}
}
You can change it to take input of certain directory or files of certain, or no extension.
cart at kolix dot de
01-Feb-2006 03:53
*updated version
Here is my recursive read_dir function. It sorts directories and files alphabetically.
<?php
function read_dir($dir) {
$path = opendir($dir);
while (false !== ($file = readdir($path))) {
if($file!="." && $file!="..") {
if(is_file($dir."/".$file))
$files[]=$file;
else
$dirs[]=$dir."/".$file;
}
}
if($dirs) {
natcasesort($dirs);
foreach($dirs as $dir) {
echo $dir;
read_dir($dir);
}
}
if($files) {
natcasesort($files);
foreach ($files as $file)
echo $file
}
closedir($path);
}
?>
Start with:
<?php
$path="path/to/your/dir";
read_dir($path);
?>
Marijan Greguric
31-Jan-2006 01:38
Example directory tree with javascript
<script>
function pokazi(dir){
alert(dir);
x=document.getElementById(dir);
if(x.style.display == "none"){
x.style.display = "";
}else{
x.style.display = "none";
}
}
</script>
</head>
<body>
<?php
$path= "../../upload/";
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if(is_dir("$path".$file)){
echo "<div style=\"padding-left: 2px; margin: 0px; border: 1px solid #000000;\" >";
echo "|-<b><a href=\"#\" onClick=pokazi(\"$file\")>$file</a></b><br />\n";
$id="$file";
dirT("$path$file/",$id);
}else{
echo "$file<br />\n";
}
}
}
echo "</div>";
closedir($handle);
}
function dirT($path,$id){
echo "<div style=\"padding-left: 2px; margin: 2px; border: 1px solid #000000; display:none \" id=\"$id\">";
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if(is_dir("$path".$file)){
echo "<div style=\"padding-left: 10px; margin: 2px; border: 1px solid #000000; \" >";
echo "|-<a href=\"#\" onClick=pokazi(\"$file\")>$file</a><br />\n";
$id="$file";
dirT("$path$file/",$id);
}else{
echo " --$file<br />\n";
}
}
}
echo "</div>";
closedir($handle);
}
echo "</div>";
}
?>
larry11 at onegentleman dot biz
21-Jan-2006 02:23
After hours of work I found an error in the sort routine sumitted. The correction lies in the fact you need $path.$file to diffirentiate from [dir] or [file] in the routines
$path = "music/Holly Dunn/";
$dh = opendir($path);
while (false !== ($file=readdir($dh)))
{
if (substr($file,0,1)!=".")
{
if (is_dir($path.$file))
$dirs[]=$file.'/';
else
$files[]=$file;
}
}
@closedir($dh);
if ($files)
natcasesort($files);
if ($dirs)
natcasesort($dirs);
$files=array_merge($dirs,$files);
foreach ($files as $file)
echo "<a href=$file>$file</a><br />";
?>
info [AT] thijsbaars [DOT] NL
29-Dec-2005 03:39
wrote this piece. I use it to validate my $_GET variables.
It firsts gets the variable $page, cheks if that variable is assinged (on the main index, it is not).
if it is assinged, it checks if it equals one of the found directory's, and then i assigns the variable definatly.
this is a rather secure way of validating the $_GET variable, which on my site should only have the name of one of my directory's.
<?php
$page = $_GET['a'];
if ($page != "" || !empty($page))
{
$handle=opendir('albums');
while (false!==($file = readdir($handle)))
{
if (substr($file,0,1)!=".") //do not look up files or directory's which start with a dot
{
if(!eregi("thumb",$file)) // all files and dir's containign the word thumb are filtred out
{
$path = "albums/" . $file;
if(is_dir($path)) // only directory's will get listed
{
if($page != $file) die("wrong argument! only a existing album may be set"); // validates input
else
{
$page = "albums/" . $page;
$thumb = $page . thumb;
}
}
}
}
}
closedir($handle);
}
doom at inet dot ua
16-Dec-2005 07:10
How i make my directory tree for print (you can make array)
<?php
$First_Dir = "./my_big_dir";
read_dir($First_Dir);
/**** functions part ****/
function read_dir($name_subdir)
{
$dh = opendir($name_subdir);
while(gettype($file = readdir($dh))!=boolean)
{
if ($file != '..')
if ($file != '.')
{
$current_element = $name_subdir."/".$file;
if(is_dir($current_element))
{
echo $current_element."<br>";
read_dir($current_element);
}
}
}
closedir($dh);
}
?>
Best free :-)
Absynthe
30-Nov-2005 02:18
This is a function to read all the directories and subdirectories of a folder. It returns an array with all the paths.
<?PHP
$root = "root";
function arborescence($root)
{
$folders = array();
$path = "$root";
$i = 0;
// Vrification de l'ouverture du dossier
if ($handle = opendir("./".$path))
{
// Lecture du dossier
while ( false !== ($file = readdir($handle)) )
{
if (filetype($path."/".$file) == 'dir')
{
if ($file != '..' && $file != '.' && $file != '' && (substr($file,0,1) != '.'))
{
$folders[$i] = "./".$path."/".$file;
$i++;
}
}
}
closedir($handle);
// Recherche de tous les sous dossiers pour chaque dossiers
for ( $u = 0; $u < $i; $u++ )
{
if ($handle = opendir($folders[$u]))
{
// Lecture du dossier
while ( false !== ($file = readdir($handle)) )
{
if (filetype("./".$folders[$u]."/".$file) == 'dir')
{
if ($file != '..' && $file != '.' && $file != '' && (substr($file,0,1) != '.'))
{
$folders[$i] = $folders[$u]."/".$file;
$i++;
}
}
}
closedir($handle);
}
}
}
return $folders;
}
?>
Martin dot Grossberger at andtek dot com
25-Nov-2005 10:39
to ES:
i believe you are mistaken. readdir() returns a string (the filename), not an array
therefor, the open_dir function should be like this:
function open_dir($dir) {
$this->dir = $dir;
$handle = opendir($this->dir);
if($handle) {
while(false !== ($file = readdir($handle)) {
if($file != "." && $file != "..") {
$files[] = $file;
}
}
sort($files);
foreach($files as $file) {
echo $file."<br>";
}
}
}
ES
25-Nov-2005 07:58
A simple class to read a dir, and display the files. And will automatically sort the files Alphabetically. Very simple indeed. But thought I'd share (one of my first classes aswell) :)
<?php
class list_dir
{
var $dir;
function open_dir($dir)
{
$handle = opendir($this->dir);
if($handle)
{
while(false !== ($files = readdir($this->dir)))
{
if($files != "." && $files != "..")
{
sort($files);
foreach($files as $file)
{
echo $file."<br>";
}
}
}
}
}
function close_dir()
{
closedir($this->dir);
}
}
?>
Usage:
<?php
$list = new list_dir;
$list->open_dir("/path/to/directory/");
$list->close_dir();
?>
24-Sep-2005 05:31
To beanz at gangstersonline dot co dot uk, just a little optimization tip.
You have make a lot of eregi calls in a sequence of elseif statements. That means that for a worst case, you'll be making 8 eregi calls per iteration. Instead of using a bunch of conditional eregi() calls (which is much slower compared to preg_match or strpos), you could do the following:
<?php
if ($i)
//create a mapping array
$ext_map = array('exe' => 'exe', 'bmp' => 'image1', etc....)
foreach($tab as $rep) {
//use one preg_match to grab the extention
if (preg_match('/\.([a-z]+)$/', $rep, $matches) {
$ext = $ext_map[$matches[1]];
} else {
$ext = 'text';
}
//rest of code...
}
?>
So instead of a worst case of n eregi calls per iteration, you're guaranteed only one preg_match per call. Or possibly even faster than preg_match:
$ext = $ext_map[substr($rep, strrpos($rep, '.'))];
beanz at gangstersonline dot co dot uk
19-Sep-2005 11:43
A little script I made for my home page on local host. It displays the contents of the folder in which the script is placed, and adds some images in a folder supplied (which is elminated from the file list)
<?PHP
$list_ignore = array ('.','..','images','Thumbs.db','index.php'); // Windows server, so thumbs.db shows up :P
$handle=opendir(".");
$dirs=array();
$files=array();
$i = 0;
while (false !== ($file = readdir($handle))) {
if (!in_array($file,$list_ignore)) {
if(!eregi("([.]bak)",$file)) { // ignore backup files
if(is_dir($file)) {
$dirs[]=$file;
} else {
$files[]=$file;
}
$i++;
}
}
}
closedir($handle);
$tab=array_merge($dirs,$files);
if ($i) {
foreach ($tab as $rep) {
if(eregi("[.]exe", $rep)) {
$ext="exe";
} elseif(eregi("[.]php", $rep)) {
$ext="php";
} elseif(eregi("[.]bmp", $rep)) {
$ext="image2";
} elseif(eregi("[.]doc", $rep)) {
$ext="word";
} elseif(eregi("[.]xls", $rep)) {
$ext="excel";
} elseif(eregi("([.]zip)|([.]rar)", $rep)) {
$ext="archive";
} elseif(eregi("([.]gif)|([.]jpg)|([.]jpeg)|([.]png)", $rep)) {
$ext="image";
} elseif(eregi("([.]html)|([.]htm)", $rep)) {
$ext="firefox";
} elseif($rep[folder]==true) {
$ext="folder";
} else {
$ext="text";
}
echo ('<a href="'.$rep.'"><img src="images/'.$ext.'.bmp" border="0"> '.$rep.'</a><br>');
}
} else {
echo "No files";
}
?>
anonymous at anonymous dot org
09-Aug-2005 09:11
My simple function for clean up .svn directory
It doesn't recursive, but exec rmdir :P
function clean_svn( $stack )
{
$dir_list = array();
$rm_list = array();
// translate to real path
$stack = realpath( $stack );
// Initial dir_list with $stack
array_push( $dir_list, $stack );
while( $current_dir = array_pop( $dir_list ) )
{
$search = $current_dir . "\\" . "{*,.svn}";
foreach( glob( $search, GLOB_ONLYDIR | GLOB_BRACE ) as $name )
{
if( ereg( '\.svn$', $name ) )
array_push( $rm_list, $name );
else
array_push( $dir_list, $name );
}
}
// remove directory
foreach( $rm_list as $name )
{
exec( "rmdir /S /Q $name" );
}
}
Basis - basis.klown-network.com
04-Aug-2005 04:11
Alex's example was really good except for just one small part that was keeping it from displaying directories above files in alphabetical order (His has makes the directories and files mixed)
I changed 'if (is_dir($path.$file))' to 'if (is_dir($file))' and it works perfectly now. Here's the whole block.
<code>
$path = $_SERVER[DOCUMENT_ROOT];
$dh = @opendir($path);
while (false !== ($file=@readdir($dh)))
{
if (substr($file,0,1)!=".")
{
if (is_dir($file))
$dirs[]=$file.'/';
else
$files[]=$file;
}
}
&
|