SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2009
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using file_get_contents after readdir

    I'm working on a project locally to copy some information from some files into new files to make the content easier to study. I've successfully used the functions opendir and readdir to get a list of files I need from the directory but I'm not sure how to use the variable with the array to implement the file_get_comments function. Here is the function I"m using to obtain my list into an array.
    Code:
    error_reporting(E_ALL);
    function getDirectory( $path = '.', $level = 0 ){
    
        $ignore = array( 'cgi-bin', '.', '..' );
        // Directories to ignore when listing output. Many hosts
        // will deny PHP access to the cgi-bin.
    
        $dh = opendir( $path );
        // Open the directory to the handle $dh
        
        while( false !== ( $file = readdir( $dh ) ) ){
        // Loop through the directory
        
            if( !in_array( $file, $ignore ) ){
    		
            // Check that this file is not to be ignored
                
                $spaces = str_repeat( ' ', ( $level * 4 ) );
                // Just to add spacing to the list, to better
                // show the directory tree.
                
                if( is_dir( "$path/$file" ) ){
                // Its a directory, so we need to keep reading down...
              
                    echo "<strong>$spaces $file</strong><br />";
    				
                    getDirectory( "$path/$file", ($level+1) );
                    // Re-call this same function but on a new directory.
                    // this is what makes function recursive.
                
                } else {
                
                    echo "$spaces $file<br />";
                    // Just print out the filename
                
    				}					
            }    
        }
        
        closedir( $dh );
        // Close the directory handle
    }
    
    getDirectory("f:/wamp/www/courses/coursmat/contents/video-lectures/");
    This returns a list of all the files in all of the sub-directories of the video-lectures directory.
    I think a foreach statement would be used but I'm not sure how to approach it. I'd like to target only the index.html files and put them in an array for use in the file_get_contents.
    Thanks in advance.

  2. #2
    SitePoint Addict
    Join Date
    Aug 2007
    Location
    GR
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This code outputs the list in a specific format on the page. You just need to declare
    an array and add the $file variable in there. You will have to append the folder name because in order
    to read the file you must know the absolute path to it.

  3. #3
    SitePoint Zealot
    Join Date
    Sep 2009
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using the following function I have placed all of the directories and files into an array. It's been quite a process to get this far(two days!). My experience and skill level end here and I have no direction in which to go. If someone could offer some direction I would greatly appreciate it.
    Here is the function I have.
    Code:
    error_reporting(E_ALL);
    global $array_items;
    function get_dir_file_array($directory='.', $recursive= false, $include_directories_in_list = true) {
    	$array_items = array();
    	
    	if ($handle = opendir($directory)) {
    		while (false !== ($file = readdir($handle))) {
    			if ($file != "." && $file != "..") {
    				if (is_dir($directory. "/" . $file)) {
    					if($recursive) {
    						$array_items = array_merge($array_items, get_dir_file_array($directory. "/" . $file, $recursive));
    						}
    						if ( $include_directories_in_list ) {
    						
    							$file = $directory . "/" . $file;
    							print "<strong>$file</strong><br />\n";
    							$array_items[] = preg_replace("/\/\//si", "/", $file);
    						}			
    					$file = $directory . "/" . $file;
    					$array_items[] = preg_replace("/\/\//si", "/", $file);
    				} else {
    					$file = $directory . "/" . $file;
    					print "<strong>$file</strong><br />\n";
    					$array_items[] = preg_replace("/\/\//si", "/", $file);
    				}
    			}
    		}
    		closedir($handle);
    	}
    	return $array_items;
    }
    $files = get_dir_file_array("f:/wamp/www/courses/contents/video-lectures", TRUE);
    To work with the array I can use a foreach statement and it list all of my directories with the full path on one line and then all of the files within that directory on subsequent lines. There are 22 sub-directories.
    I would like to target each of the sub-directories and then the index.htm file within that directory. Then using the file_get_contents function, I would like to loop through all of the index.htm files and pull content from them. The sub-directory names will be used as the title of a lesson.
    This sounds like a lot to me but it may be simple for someone with a skill level far beyond mine.
    Thanks again


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
  •