JesseToxik Posted March 28, 2013 Share Posted March 28, 2013 Hello all, I'm new to PHP and in a bit of a jam. I have a PHP script that scans a specified folder for all the files in it. All of the files are images. It uses a foreach statement to place them all inside of hyperlinks linked to lightbox to make an image gallery. This works perfectly. Now part two of my gallery project is scanning a different file for videos. I figured it would be pretty easy to edit my existing script and conform it to video. I was wrong. Halfway through rewriting the script I realized that every video has two formats. MP4 and OGG. This will cause every video to be displayed in the gallery twice. Does anyone know how to scan a file for videos and place them on a page without making an embeded video for each format? Below is my code. <?php $videos= glob('videos/*'); natcasesort($videos); foreach($videos as $video) { echo '<video controls tabindex="0">'; echo '<source src="'.$video.'" type="video/mp4;" />'; echo '<source src="'.$video." type="video/ogg;" />'; echo '</video>'; } ?> Quote Link to comment Share on other sites More sharing options...
requinix Posted March 28, 2013 Share Posted March 28, 2013 Scan a file? You mean directory. Really confusing otherwise. Build an array beforehand of the names of files and their various extensions. Like $rawvideos = glob('videos/*.*'); natcasesort($rawvideos); $videos = array(); foreach ($rawvideos as $file) { $extension = substr($file, strrpos($file, '.') + 1); $filenoext = substr($file, 0, strlen($extension) + 1); if (!isset($videos[$filenoext])) { $videos[$filenoext] = array(); } $videos[$filenoext][] = $extension; }Then do your normal loop over $videos but you can check for extensions. Or go fancy and use another loop.$types = array( 'mp4' => 'video/mp4', 'ogg' => 'video/ogg' ); foreach ($videos as $filenoext => $extensions) { echo '<video controls tabindex="0">'; foreach ($extensions as $extension) { if (isset($types[$extension])) { echo '<source src="' . $filenoext . "." . $extension . '" type="' . $types[$extension] . '" />'; } } echo '</video>'; } Quote Link to comment Share on other sites More sharing options...
JesseToxik Posted March 28, 2013 Author Share Posted March 28, 2013 (edited) EDITED - So far it works except for one problem. After running the page through wampserver to preview it I get a black box saying "No video with supported format and MIME type found."[Firefox]. After viewing the source code this is the video line that your code is generating. <video controls tabindex="0"><source src="vide.mp4" type="video/mp4" /><source src="vide.ogg" type="video/ogg" /></video> Any idea why the video souces are vide.mp4 and vide.ogg? Edited March 28, 2013 by JesseToxik Quote Link to comment Share on other sites More sharing options...
JesseToxik Posted March 28, 2013 Author Share Posted March 28, 2013 I have actually found the problem but not sure how to fix it. This line: $filenoext = substr($file, 0, strlen($extension) + 1); It is only allowing so much added to the source. If I were to change that 1 to a 10 the source would go from vide.mp4 and vide.ogg to videos/traile.mp4 and videos/traile.ogg. Any ideas? Quote Link to comment Share on other sites More sharing options...
requinix Posted March 28, 2013 Share Posted March 28, 2013 Ah, typo. $filenoext = substr($file, 0, -strlen($extension) - 1);Negative number. If the extension is "ogg" then it should act like substr($file, 0, -4) (-1 for the period). Quote Link to comment Share on other sites More sharing options...
JesseToxik Posted March 28, 2013 Author Share Posted March 28, 2013 Changing the + 1 to a - 1 gives me this in my source code. <video controls tabindex="0"><source src="vi.mp4" type="video/mp4" /><source src="vi.ogg" type="video/ogg" /></video> Quote Link to comment Share on other sites More sharing options...
JesseToxik Posted March 28, 2013 Author Share Posted March 28, 2013 I missed the other "-" you put in there. Seems to work. Will add another video to the directory for testing. Quote Link to comment Share on other sites More sharing options...
JesseToxik Posted March 28, 2013 Author Share Posted March 28, 2013 Works like a charm If I wanted to add more extensions would I just edit this line? $types = array( 'mp4' => 'video/mp4', 'ogg' => 'video/ogg' Quote Link to comment Share on other sites More sharing options...
requinix Posted March 28, 2013 Share Posted March 28, 2013 Yep. Quote Link to comment Share on other sites More sharing options...
JesseToxik Posted March 29, 2013 Author Share Posted March 29, 2013 Ok cool thanks for the help Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.