PHP foreach video file

Discussion in 'PHP' started by JesseToxik, Mar 27, 2013.

  1. #1
    Hello everyone,

    I am in a bit of a bind since I am pretty new to PHP and have no idea how to do this.
    I currently have a bit of php code that display every image in a specific folder in an image tag linked to lightbox. works like a charm. I tried implementing this code for videos. Halfway through writing the script I ran into a problem. If I use the foreach statement to gather video files it will show each video twice(the mp4 format and the ogg format).

    Is there anyway possible to scan a folder for each video file(mp4 and ogg) and place them into a code like this
    <video poster="star.png" autoplay loop controls tabindex="0">
      <source src="video/trailer_test.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"' />
      <source src="video/trailer_test.ogg" type='video/ogg; codecs="theora, vorbis"' />
    </video>
    HTML:
    My foreach code for images is
    <?php
    $files = glob('images/misc/*');
    natcasesort($files);
    foreach($files as $file) {
      echo '<a href="'.$file.'" rel="lightbox[gallery]"><img src="'.$file.'" alt="random image" class="gallery_icon"></a>';
    }
    ?>
    Code (markup):
    Thanks everyone.
     
    JesseToxik, Mar 27, 2013 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    You will need to build a function to keep track of the name so that you can put both the mp4 and the ogg in the same content holder.

    How many videos are in this directory? This could result in a fairly large amount of memory usage if there are thousands of videos.
     
    jestep, Mar 28, 2013 IP
  3. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #3

    Personally I would just check the file type and make that work from a condition to set the returned data depending what the extension is.

    Try this out sorry I haven't tested it so let me know if it doesn't work.

    
     
    <?php
    function check_extension($file) {
       $extension = explode('.', $file);
       if($extension[1] == 'ogg' || $extension[1] == 'mp4') {
           // video
           $return = '<video poster="star.png" autoplay loop controls tabindex="0">
      <source src="video/'.$file.'" type=\'video/'.$extension[0].'; codecs="avc1.42E01E, mp4a.40.2"\' />
    </video>';    
       }else{
           // image
           $return = '<a href="'.$file.'" rel="lightbox[gallery]"><img src="'.$file.'" alt="random image" class="gallery_icon"></a>';
       }
       return $return;
    }
    $files = glob('images/misc/*');
    natcasesort($files);
    foreach($files as $file) {
       echo check_extension($file);
       //echo '<a href="'.$file.'" rel="lightbox[gallery]"><img src="'.$file.'" alt="random image" class="gallery_icon"></a>';
    }
    ?>
     
    
    PHP:
     
    HuggyStudios, Mar 28, 2013 IP
  4. JesseToxik

    JesseToxik Greenhorn

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #4
    Ah I forgot to mention that my images and videos were both in different directories.
    I will try your method and see what happens.
     
    JesseToxik, Mar 28, 2013 IP
  5. JesseToxik

    JesseToxik Greenhorn

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #5

    It won't be that many.
    Its for a painting company and I am going to have about 5 - 10 videos on a gallery page so that people may see the company in action.
     
    JesseToxik, Mar 28, 2013 IP