Listing all files in the directory

[b]code[/b]
if ( fileExist("myFile.php") )
{echo "Yes, the file which is named "myFile.php" is existed in the folder." }
else
{echo "No, the file which is named "myFile.php" is NOT existed in the folder." }

[b]result[/b]
Yes, the file which is named "myFile.php" is existed in the folder.


The code above confirm whether “myFile.php” is existed or not in the directory.

I like to get all files in the directory.

The code below doesn’t work correctly, but I hope it shows what I want.

[b]would-be code[/b]
echo list_all_files($the_directory);

[b]result[/b]
myFile.php
myFile2.php
myFile3.php

I wrote this script four or five years ago, but I think it should do what you want


<?php
function displayDirectory($TARGETdir='',$path='',  $fileTypes= array('php','html','htm')){
 	$dir 		= 	$path.$TARGETdir;  
 	$fileCOUNT	=	0;
	if (is_dir($dir)) {
	    echo "<ul>\
";
	    if ($dh = opendir($dir)) {
	        while (($file = readdir($dh)) !== false) {
	        	if(strpos($file, ".")!=0 && !ereg("^(Temp)",$file)) {       		 
	        	 $test=explode(".", $file);
	        	 if (!$fileTypes || in_array($test[1], $fileTypes ) ){ // only lists php
	        	         	 $fileCOUNT++;
	        	 	 		echo '<li><a href="',$dir,'/',$file,'">', $file,'</a></li>',"\
";}
	        	}
	        }
	        closedir($dh);
	        echo '</ul>',"\
";
	 }
	    if ($fileCOUNT!=1){
	    	$pv="are";
	        $pn="s";
	        }
	     else{
	        $pv="is";
	        $pn="";
	        }
	    echo "<p>There $pv $fileCOUNT file$pn  in the $TARGETdir folder.</p> \
";
	}

}
displayDirectory( 'portfolio','',false);

?>

the first argument is the directory name, the second is the path to the directory ( relative to the directory itself), the last is either an array of the file types you wish to list or false for list all. BONUS it adjust its grammar to indicate 0,1, or multiple files.

hope that helps

using GLOB might be easier…

It helps a lot for me.
But I am still struggling with your code.
Index.php which is one of my file has the code above.
I put another file named test.php in the same directory.
When I browse the file “index.php” which has your code,
expecting it lists index.php and test.php
nothing shown. it’s all blank.

I guess I put some value into $targetDir or $path.
How can I get my target result below with your code?

[b]target result[/b]
index.php
test.php

just a thought…


<?php
$dir = "*.txt";  /* adjust the directory and/or file type as desired */
foreach(glob($dir) as $file)   {echo $file . "<br />";  }
?>

its a function … delete : displayDirectory( ‘portfolio’,‘’,false);

displayDirectory( directory name ,path to the directory, file types to list);

  • the first argument is required, all other arguments are optional
  • if the second argument is not provided it will look for director in the folder of the file when the function is actually coded; any path argument provided should also be relative to THAT file
  • the third augment is an ARRAY or false. False will display all files in the directory, regardless of type. If you provide an array it will list only the file types in the array

for example, lets say you have a root directory, which contains two sub directories : “dirOne” and “dirTwo”, the file with the code I provided is dirOne and you want a listing of JPGs, GIFs, and PNGs in dirTwo. What you would have to do , once you have included the file with my function is call it like this:

displayDirectory(‘dirTwo’,‘./’,array(‘jpg’,jpeg’,‘gif’,‘png’));

Try your test again, but this time do:
displayDirectory(‘theNameOfTheDirectoryThatHasIndexPHP’,‘./’ );

substituting ‘theNameOfTheDirectoryThatHasIndexPHP’ for the appropriate director name, of course.
include the rest in your code. BTW , if you use the include command , remember that the path given should be relative to the included, not the actual file. after your file is included all you need to do is call displayDirectory(); with the appropriate arguments anytime you want a directory listing.

I have a php file named “displayDir.php” in the 1st directory named “x-test” from my htdocs.
You can see it at http://dot.kr/x-test/displayDir.php at the moment.

<?php
function displayDirectory($TARGETdir='',$path='',  $fileTypes= array('php','html','htm')){
 	$dir 		= 	$path.$TARGETdir;  
 	$fileCOUNT	=	0;
	if (is_dir($dir)) {
	    echo "<ul>\
";
	    if ($dh = opendir($dir)) {
	        while (($file = readdir($dh)) !== false) {
	        	if(strpos($file, ".")!=0 && !ereg("^(Temp)",$file)) {       		 
	        	 $test=explode(".", $file);
	        	 if (!$fileTypes || in_array($test[1], $fileTypes ) ){ // only lists php
	        	         	 $fileCOUNT++;
	        	 	 		echo '<li><a href="',$dir,'/',$file,'">', $file,'</a></li>',"\
";}
	        	}
	        }
	        closedir($dh);
	        echo '</ul>',"\
";
	 }
	    if ($fileCOUNT!=1){
	    	$pv="are";
	        $pn="s";
	        }
	     else{
	        $pv="is";
	        $pn="";
	        }
	    echo "<p>There $pv $fileCOUNT file$pn  in the $TARGETdir folder.</p> \
";
	}

}

echo 'This is the php file named "displayDir".';
echo [COLOR="#FF0000"]displayDirectory('x-test','./',false);[/COLOR]

?>

Now I am calling the displayDirectory function with displayDirectory(‘x-test’,‘./’,false);, but nothing happend as you see at http://dot.kr/x-test/displayDir.php.
The code above is the all code in the file “displayDir.php”.

How should I modify the code above for displaying all files in the directory?

<?php
function displayDirectory($TARGETdir='',$path='',  $fileTypes= array('php','html','htm')){
 	$dir 		= 	$path.$TARGETdir;
 	$fileCOUNT	=	0;
	if (is_dir($dir)) {
	    echo "<ul>\
";
	    if ($dh = opendir($dir)) {
	        while (($file = readdir($dh)) !== false) {
	        	if(strpos($file, ".")!=0 && !ereg("^(Temp)",$file)) {       		
	        	 $test=explode(".", $file);
	        	 if (!$fileTypes || in_array($test[1], $fileTypes ) ){ // only lists php
	        	         	 $fileCOUNT++;
	        	 	 		echo '<li><a href="',$dir,'/',$file,'">', $file,'</a></li>',"\
";}
	        	}
	        }
	        closedir($dh);
	        echo '</ul>',"\
";
	 }
	    if ($fileCOUNT!=1){
	    	$pv="are";
	        $pn="s";
	        }
	     else{
	        $pv="is";
	        $pn="";
	        }
	    echo "<p>There $pv $fileCOUNT file$pn  in the $TARGETdir folder.</p> \
";
	}

}

The above is the function which is wrote by dresden_phoenix four or five years ago.
The below which is testing various way of paths AND directories is for calling the function and displaying the result of the function.

echo 'This is the php file named "displayDir2.php".';

echo displayDirectory('x-test','./',false);
echo displayDirectory('x-test','',false);
echo displayDirectory('x-test','./');
echo displayDirectory('x-test','');
echo displayDirectory('x-test');

echo displayDirectory('y-test','./',false);
echo displayDirectory('y-test','',false);
echo displayDirectory('y-test','./');
echo displayDirectory('y-test','');
echo displayDirectory('y-test');
?>

The function produces nothing at http://dot.kr/x-test/displayDir2.php.
How can I make the function produces something?

  1. Are you passing the proper parameters to the function?
  2. Show us the code where you actually implement the function

The below is the all code at the page of http://dot.kr/x-test/displayDir2.php.

<?php
function displayDirectory($TARGETdir='',$path='',  $fileTypes= array('php','html','htm')){
 	$dir 		= 	$path.$TARGETdir;  
 	$fileCOUNT	=	0;
	if (is_dir($dir)) {
	    echo "<ul>\
";
	    if ($dh = opendir($dir)) {
	        while (($file = readdir($dh)) !== false) {
	        	if(strpos($file, ".")!=0 && !ereg("^(Temp)",$file)) {       		 
	        	 $test=explode(".", $file);
	        	 if (!$fileTypes || in_array($test[1], $fileTypes ) ){ // only lists php
	        	         	 $fileCOUNT++;
	        	 	 		echo '<li><a href="',$dir,'/',$file,'">', $file,'</a></li>',"\
";}
	        	}
	        }
	        closedir($dh);
	        echo '</ul>',"\
";
	 }
	    if ($fileCOUNT!=1){
	    	$pv="are";
	        $pn="s";
	        }
	     else{
	        $pv="is";
	        $pn="";
	        }
	    echo "<p>There $pv $fileCOUNT file$pn  in the $TARGETdir folder.</p> \
";
	}

}
echo 'This is the php file named "displayDir2.php".';

echo displayDirectory('x-test','./',false);
echo displayDirectory('x-test','',false);
echo displayDirectory('x-test','./');
echo displayDirectory('x-test','');
echo displayDirectory('x-test');

echo displayDirectory('y-test','./',false);
echo displayDirectory('y-test','',false);
echo displayDirectory('y-test','./');
echo displayDirectory('y-test','');
echo displayDirectory('y-test');
?>

add these two lines to the end of your file…


$dir="../x-text";
displayDirectory($dir, "",  $fileTypes= array('php','html','htm'));

I added your two lines to the end of my file at http://dot.kr/x-test/displayDir2.php and save as displayDir3.php, you can see it at http://dot.kr/x-test/displayDir3.php .
But there is sadly no difference betwwen displayDir2.php and displayDir3.php.

expecting something happen, I even tried to add echo like the following.

$dir="../x-text"; 
[COLOR="#FF0000"]echo[/COLOR] displayDirectory($dir, "",  $fileTypes= array('php','html','htm')); 

But nothing happened.

make a file named test.php

put this in the file


<?php
$dir = "*.txt";  /* adjust the directory and/or file type as desired */
foreach(glob($dir) as $file)   {echo $file . "<br />";  }
?>

upload the file
point your browser to the file
tell us what happens

I made a directory named “displayDir” in the directory of “x-test”.
I made a index.php with your code above.
The result of it is at http://dot.kr/x-test/displayDir/.
Happily, it works fine.

The below is also work fine at http://dot.kr/x-test/displayDir/test.php.


<?php
$dir = "*.*";  /* adjust the directory and/or file type as desired */
foreach(glob($dir) as $file)   {echo $file . "<br />";  }  
?> 

Thank you very much, litebearer.