opendir

Discussion in 'PHP' started by aadi92s, Apr 8, 2010.

  1. #1
    hi,
    i want a function opendir folder and get all php files name and size... if hi have more folder in this folder then hi open thease folders and get php files name and get size ......
    thanx in advance
     
    aadi92s, Apr 8, 2010 IP
  2. Dakuipje

    Dakuipje Peon

    Messages:
    931
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You should read this:

    http://php.net/manual/en/function.readdir.php

    <?php
    if ($handle = opendir('.')) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..") {
                echo "$file\n";
            }
        }
        closedir($handle);
    }
    ?>
    PHP:
    Then simply use is_dir() function to see if the filename isnt in fact a dir, if it is simply loop that in the same function
     
    Dakuipje, Apr 8, 2010 IP
  3. aadi92s

    aadi92s Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    i try this but have many problem one in loop and main problm is get size is not working
    
    $dir1 ='files/'; 
    		$dh1 = opendir($dir1);
    		
    		while (($file1 = readdir($dh1)) !== false) {
    	
    		if($file1!="." && $file1!=".." && $file1!="Thumbs.db" ){
    
        $fileext = substr(strrchr($file1, "."), 1);
    if (!$fileext){ // if a folder
    $dir2 =$dir1."".$file1."/"; 
    		$dh2 = opendir($dir2);		
    		while (($file2 = readdir($dh2)) !== false) {	
    		if($file2!="." && $file2!=".." && $file2!="Thumbs.db" ){
    		 $fileext2 = substr(strrchr($file2, "."), 1);
    ////////////////////////////////////////
    	if (!$fileext2){ // if a folder
    		 $dir3 =$dir2."".$file2."/"; 
    		 $dh3 = opendir($dir3);
    		 while (($file3 = readdir($dh3)) !== false) {
    		 if($file3!="." && $file3!=".." && $file3!="Thumbs.db" ){
    		 $fileext3 = substr(strrchr($file3, "."), 1);
    		 if($fileext3 == "php"){
    		 echo "3rd folder files are ".$file3;
    		 echo "<br>";
    		 }
    		 }}closedir($dh2);	 }
    /////////////////////////////////////////	 
    		 
    		 
    		 if($fileext2 == "php"){
    		 	echo "2nd folder files are ".$file2;
    		echo "<br>";
    		}
    		///////////
    		}	closedir($dh3);	} // if end
    
       
    
    }
    if($fileext == "php"){
    echo "1st folder files are ".$file1;
    echo "<br>";
     }
     
     }}
     closedir($dh1);	
    
    PHP:
     
    Last edited: Apr 8, 2010
    aadi92s, Apr 8, 2010 IP
  4. itmedia

    itmedia Peon

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4


    I believe Dakuipje without beeing sure
     
    itmedia, Apr 8, 2010 IP
  5. aadi92s

    aadi92s Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    i have done this
    (one have one problem get file size)
    function ListFolder($path)
    {
    
        //using the opendir function
        $dir_handle = @opendir($path) or die("Unable to open $path");
       
        //Leave only the lastest folder name
        $dirname = end(explode("/", $path));
       
        //display the target folder.
       $dirname;
       
        while (false !== ($file = readdir($dir_handle)))
        {
            if($file!="." && $file!="..")
            {
                if (is_dir($path."/".$file))
                {
                    //Display a list of sub folders.
                    ListFolder($path."/".$file);
                }
                else
                {			
    			$fileext = substr(strrchr($file, "."), 1);
    			if($fileext=="php" )//if files are php
    			{
                    //Display a list of files.
                   echo $file;
    				echo "<br>";
                }
    			}
            }
        }
        
       
        //closing the directory
        closedir($dir_handle);
    }
    PHP:
    <script language="JavaScript" type="text/javascript">
    <!--
    dmxListToTree({
      bullets : 'plusminus',
      icons   : true,
      struct  : false,
      objId   : 'FolderView'
    });
    //-->
    </script>
    <div class="dmxtree" id="FolderView">
    <?php ListFolder("files/"); ?>
    </div>
    HTML:
     
    aadi92s, Apr 8, 2010 IP