0

I'm using a php video gallery from codeboxx in an attempt to see all the videos my security camera is uploading to my hosting. My camera makes many subfolders for dates and hours which I can't keep anticipating. What I've been trying to get is for this script to include subfolders:

  <body>
    <div id="vid-gallery"><?php
      // (A) GET ALL VIDEO FILES FROM THE GALLERY FOLDER
      $dir = __DIR__ . DIRECTORY_SEPARATOR . "gallery" . DIRECTORY_SEPARATOR;
      $videos = glob($dir . "*.{webm,mp4,ogg}", GLOB_BRACE);

      // (B) OUTPUT ALL VIDEOS
      if (count($videos) > 0) { foreach ($videos as $vid) {
        printf("<video src='gallery/%s'></video>", rawurlencode(basename($vid)));
      }}
    ?></div>
  </body>

I think I can get the first part to work in scanning 4 subfolders deep by me doing this:

      $dir = __DIR__ . DIRECTORY_SEPARATOR . "{gallery,gallery/**,gallery/**/**,gallery/**/**/**,gallery/**/**/**/**}" . DIRECTORY_SEPARATOR;

Now I think this line is giving me a problem as I can't seem to get this src get videos in the subdirectories and wildcards don't seem to work:

  printf("<video src='gallery/%s'></video>", rawurlencode(basename($vid)));

I've tried to look up how to do it and I've seen array/echo/recursive functions, but I don't know how to implement anything. I appreciate any help.

  • A combo of `recursiveDirectoryIterator` and recurseiveIterator` will do it – Professor Abronsius Sep 16 '21 at 18:24
  • 1
    Does this answer your question? [List all the files and folders in a Directory with PHP recursive function](https://stackoverflow.com/questions/24783862/list-all-the-files-and-folders-in-a-directory-with-php-recursive-function) – Nico Haase Sep 17 '21 at 07:05

1 Answers1

0

Untested but along the right lines - a combination of recursiveIterator & recursiveDirectoryIterator ought to be enough to list all the files - when combined further with pathinfo to find the file extension you can do away with the glob or preg_match

$dir=sprintf('%s/gallery',__DIR__);
$exts=array('webm','ogg','mp4');

$files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $dir ), RecursiveIteratorIterator::SELF_FIRST );
if( is_object( $files ) ){
    foreach( $files as $name => $file ){
        if( !$file->isDir() && !in_array( $file->getFileName(), array('.','..') ) ){
            
            if( in_array( pathinfo( $file->getFileName(), PATHINFO_EXTENSION ),$exts ) ){
                echo $file->getFileName() . '<br />';
            }
        }
    }
}

An update that loads the videos as per the request

$dir=sprintf('%s/gallery', __DIR__ );
$exts=array('webm','ogg','mp4');

$files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $dir ), RecursiveIteratorIterator::SELF_FIRST );
if( is_object( $files ) ){
    foreach( $files as $name => $file ){
        if( !$file->isDir() && !in_array( $file->getFileName(), array( '.', '..' ) ) ){
            
            if( in_array( pathinfo( $file->getFileName(), PATHINFO_EXTENSION ),$exts ) ){
                /*
                    For my test the script is running within an `aliased` folder
                    outside of the directory root. The gallery folder and it's
                    sub-folders are within this alised directory.
                    
                    Roughly the structure is:
                    
                    c:\wwwroot\sites\myWebsite ~ this is the directory root
                    
                    The aliased folder - as `demo`:
                    c:\wwwroot\content\
                    
                    The working directory:
                    c:\wwwroot\content\stack 
                    
                    with the gallery
                    c:\wwwroot\content\stack\gallery etc
                    
                    So - I needed to remove the absolute portion of the returned filepaths
                       c:\wwwroot\content
                */
                $rel_filepath=str_replace( 
                    array( realpath( getcwd() ), '\\' ),
                    array( '', '/' ),
                    $file->getPathName()
                );
                
                
                $rel_filepath=ltrim( $rel_filepath, '/' );
                
                
                printf( '<h5>%1$s</h5><video src="%1$s" controls></video>', $rel_filepath );
            }
        }
    }
}

Which resulted in the following display:

Example output

Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
  • This looks very good, but I am sub par because I have tried to get video to output and I can't seem to get the gallery working. What you did successfully accessed subfolders for sure and I have a page of all the files being displayed. How do I combine what you did with getting each filename in – theunfortunatepanda Sep 17 '21 at 01:14
  • You need to remove a portion of the filepath as they will be absolute filepaths rather than relative. Without seeing more of the returned strings & without knowing your server setup ( directory root etc ) it is tough to say what you need to remove – Professor Abronsius Sep 17 '21 at 06:34