Get the content of index.htm files from 24 subdirectories

Hey. I’ve spent over a week on this now and I just can’t get anything I’ve tried to work. This is a project that I wanted to do on my own to capture you tube video lectures all on one page to allow me to take notes from them without going back an forth the get to the lectures. I’ve downloaded the lectures locally to this path.

F:/wamp/www/course/cs600/video-lectures

I’ve tried many code blocks I’ve found on the internet to iterate through the directories and return 24 arrays, but I don’t know how to work with them to get them to sort and return one video for each sub-directories index.htm file. I also have the code that successfully captures one video if I give it the direct path the the sub-directory. I think I’m too close to see this clearly now or my approach is too convoluted. I’m a php novice and I’m afraid that I have bit off a bit more that I can chew. Any help getting this done would be appreciated as I my classes start Monday.
Here is the one iteration function that return more info that I need but it gets into the sub-directories that I do need.

error_reporting(E_ALL);
function get_lecture_videos($directory, $filter=FALSE)
 {
     if(substr($directory,-1) == '/')
     {
         $directory = substr($directory,0,-1);
     }
     if(!file_exists($directory) || !is_dir($directory))
     {
         return FALSE;
     }elseif(is_readable($directory))
     {
         $directory_list = opendir($directory);
         while($file = readdir($directory_list))
         {
             if($file != '.' && $file != '..')
             {
                 $path = $directory.'/'.$file;
                if(is_readable($path))
                 {
                   $subdirectories = explode('/',$path);
                     if(is_dir($path))
                     {
                        $directory_tree[] = array(
                             'path'      => $path,
                             'name'      => end($subdirectories),
                             'kind'      => 'directory',
                             'content'   => get_lecture_videos($path, $filter));
                     }elseif(is_file($path))
                     {
                         $extension = end(explode('.',end($subdirectories)));
                        // $filename = end(explode('/', end($subdirectories)));
                        
                         
                         if($filter === FALSE || $filter == $extension)
                         {
                             $directory_tree[] = array(
                            'path'        => $path,
                             'name'        => end($subdirectories),
                             'extension' => $extension,
                             'size'        => filesize($path),
                             'kind'        => 'file');
                         }
                     }
                 }
             }
         }
         closedir($directory_list);
         return $directory_tree;
     }else{
         return FALSE;
     }
}

I do not know how to work with all of the arrays the code above produces.
Here is the code to display one video successfully. I have no idea what is going on with the foreach statement output but it works.

function video_scan($directory) {
$input = file_get_contents($directory);
preg_match_all('#(http://www.youtube.com)?/(v/([-|~_0-9A-Za-z]+)|watch\\?v\\=([-|~_0-9A-Za-z]+)&?.*?)#i',$input,$output);
echo "<ul>";
foreach ($output[2] as $video_id) {
    if (!isset($video[$video_id])) {
    $video[$video_id]=true;
	//echo $video_id;
$embed_code = '<object width="560" height="340"><param name="movie" value="http://www.youtube.com/v/'.$video_id.'&hl=en&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'.$video_id.'&hl=en&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="340"></embed></object>';
echo $embed_code.'<br>';
        }
    }
}
$get_vids = video_scan('F:/wamp/www/course/cs600/video-lectures/lecture-1/index.htm');


Your talking about recursion above.

Since it looks like your running this on a local machine you can always use exec() to list all the files with their paths and stick them in a text file. Then open the text file with fopen(), read each line, and build your output. Make sure the php executable is in your path.

Simple example for Windows / Linux just comment out the exec() that you don’t need for your operating system.



<?php

#windows example
exec('dir f:\\pathtofiles\\index*.htm /s /b > filelist.txt');

#linux example
exec('find /pathtofiles/ -name \\'index.htm\\' > filelist.txt');

$infile = fopen('filelist.txt');

... read each line of the file, at this point you could use explode() if need be, format your new output and write it to an $outfile.htm

fclose($infile);


?>


It’s not an answer to your recursion question, but, an alternate route.

WOW! That took me in a direction I didn’t expect to go. Everything I’ve learned about PHP has been used server side. Even if it is local. I’ve never encountered the exec function.
Anyway, I’ll give it a go.

Recursion seems to be great for gaining a list of files and directories but manipulating the returns for my purposes, seems to be heavy in code.

Thanks for the input.

I’m sorry but the above solution doesn’t look like it will help me as there are 24 different lecture sub-folders. It might be fine if it was a one time occurrence but this will be happening about 6 times per year. If anyone else has any ideas it would be greatly appreciated. Thanks

Maybe have a look at the glob() function to search through the folders for the index.htm files.


$files = glob('F:/wamp/www/course/cs600/video-lectures/lecture-*/index.htm');

You could then use a foreach loop and your video_scan() function.

Very Nice! Thank you. I’m able to get all the contents from each index file now I just have to parse out the video. Thank you!

One more quick question. The videos are ordered by the name which has a number in it like Lec 1. They are all ordered from 1 then it jumps to 10 and at 20 there is Lec 2. Any way to force the human readable order?
Thanks again.

I got it with


```php
natsort($files);

Thanks for that tip too! I didn’t know you could use wildcards on directory names with glob(), although I’ve seen it used recursively.