SitePoint Sponsor

User Tag List

Results 1 to 14 of 14

Hybrid View

  1. #1
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    listing all files in the directory

    Code:
    code
    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." }
    
    result
    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.
    Code:
    would-be code
    echo list_all_files($the_directory);
    
    result
    myFile.php
    myFile2.php
    myFile3.php

  2. #2
    Community Advisor silver trophybronze trophy
    dresden_phoenix's Avatar
    Join Date
    Jun 2008
    Location
    Madison, WI
    Posts
    2,804
    Mentioned
    34 Post(s)
    Tagged
    2 Thread(s)
    I wrote this script four or five years ago, but I think it should do what you want

    Code:
    <?php
    function displayDirectory($TARGETdir='',$path='',  $fileTypes= array('php','html','htm')){
     	$dir 		= 	$path.$TARGETdir;  
     	$fileCOUNT	=	0;
    	if (is_dir($dir)) {
    	    echo "<ul>\n";
    	    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>',"\n";}
    	        	}
    	        }
    	        closedir($dh);
    	        echo '</ul>',"\n";
    	 }
    	    if ($fileCOUNT!=1){
    	    	$pv="are";
    	        $pn="s";
    	        }
    	     else{
    	        $pv="is";
    	        $pn="";
    	        }
    	    echo "<p>There $pv $fileCOUNT file$pn  in the $TARGETdir folder.</p> \n";
    	}
    
    }
    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

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  4. #4
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dresden_phoenix View Post
    Code:
    <?php
    function displayDirectory($TARGETdir='',$path='',  $fileTypes= array('php','html','htm')){
     	$dir 		= 	$path.$TARGETdir;  
     	$fileCOUNT	=	0;
    	if (is_dir($dir)) {
    	    echo "<ul>\n";
    	    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>',"\n";}
    	        	}
    	        }
    	        closedir($dh);
    	        echo '</ul>',"\n";
    	 }
    	    if ($fileCOUNT!=1){
    	    	$pv="are";
    	        $pn="s";
    	        }
    	     else{
    	        $pv="is";
    	        $pn="";
    	        }
    	    echo "<p>There $pv $fileCOUNT file$pn  in the $TARGETdir folder.</p> \n";
    	}
    
    }
    displayDirectory( 'portfolio','',false);
    
    ?>
    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?
    Code:
    target result
    index.php
    test.php

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just a thought...

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

  6. #6
    Community Advisor silver trophybronze trophy
    dresden_phoenix's Avatar
    Join Date
    Jun 2008
    Location
    Madison, WI
    Posts
    2,804
    Mentioned
    34 Post(s)
    Tagged
    2 Thread(s)
    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.

  7. #7
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.
    Code:
    <?php
    function displayDirectory($TARGETdir='',$path='',  $fileTypes= array('php','html','htm')){
     	$dir 		= 	$path.$TARGETdir;  
     	$fileCOUNT	=	0;
    	if (is_dir($dir)) {
    	    echo "<ul>\n";
    	    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>',"\n";}
    	        	}
    	        }
    	        closedir($dh);
    	        echo '</ul>',"\n";
    	 }
    	    if ($fileCOUNT!=1){
    	    	$pv="are";
    	        $pn="s";
    	        }
    	     else{
    	        $pv="is";
    	        $pn="";
    	        }
    	    echo "<p>There $pv $fileCOUNT file$pn  in the $TARGETdir folder.</p> \n";
    	}
    
    }
    
    echo 'This is the php file named "displayDir".';
    echo displayDirectory('x-test','./',false);
    
    ?>
    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?

  8. #8
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <?php
    function displayDirectory($TARGETdir='',$path='',  $fileTypes= array('php','html','htm')){
     	$dir 		= 	$path.$TARGETdir;  
     	$fileCOUNT	=	0;
    	if (is_dir($dir)) {
    	    echo "<ul>\n";
    	    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>',"\n";}
    	        	}
    	        }
    	        closedir($dh);
    	        echo '</ul>',"\n";
    	 }
    	    if ($fileCOUNT!=1){
    	    	$pv="are";
    	        $pn="s";
    	        }
    	     else{
    	        $pv="is";
    	        $pn="";
    	        }
    	    echo "<p>There $pv $fileCOUNT file$pn  in the $TARGETdir folder.</p> \n";
    	}
    
    }
    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.

    Code:
    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?

  9. #9
    SitePoint Enthusiast
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1. Are you passing the proper parameters to the function?
    2. Show us the code where you actually implement the function

  10. #10
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by litebearer View Post
    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.

    Code:
    <?php
    function displayDirectory($TARGETdir='',$path='',  $fileTypes= array('php','html','htm')){
     	$dir 		= 	$path.$TARGETdir;  
     	$fileCOUNT	=	0;
    	if (is_dir($dir)) {
    	    echo "<ul>\n";
    	    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>',"\n";}
    	        	}
    	        }
    	        closedir($dh);
    	        echo '</ul>',"\n";
    	 }
    	    if ($fileCOUNT!=1){
    	    	$pv="are";
    	        $pn="s";
    	        }
    	     else{
    	        $pv="is";
    	        $pn="";
    	        }
    	    echo "<p>There $pv $fileCOUNT file$pn  in the $TARGETdir folder.</p> \n";
    	}
    
    }
    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');
    ?>

  11. #11
    SitePoint Enthusiast
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    add these two lines to the end of your file...
    PHP Code:
    $dir="../x-text";
    displayDirectory($dir"",  $fileTypes= array('php','html','htm')); 

  12. #12
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by litebearer View Post
    add these two lines to the end of your file...
    PHP Code:
    $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.
    Code:
    $dir="../x-text"; 
    echo displayDirectory($dir, "",  $fileTypes= array('php','html','htm'));
    But nothing happened.
    Last edited by dotJoon; Jun 19, 2013 at 05:50. Reason: editting

  13. #13
    SitePoint Enthusiast
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    make a file named test.php

    put this in the file
    PHP Code:
    <?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

  14. #14
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by litebearer View Post
    make a file named test.php

    put this in the file
    PHP Code:
    <?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 Code:
    <?php
    $dir 
    "*.*";  /* adjust the directory and/or file type as desired */
    foreach(glob($dir) as $file)   {echo $file "<br />";  }  
    ?>
    Thank you very much, litebearer.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •