[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.
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.
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?
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:
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');
?>
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.