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>'; } ?> Link to comment https://forums.phpfreaks.com/topic/276243-php-foreach-video-in-file/ 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>'; } Link to comment https://forums.phpfreaks.com/topic/276243-php-foreach-video-in-file/#findComment-1421537 Share on other sites More sharing options...
JesseToxik Posted March 28, 2013 Author Share Posted March 28, 2013 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? Link to comment https://forums.phpfreaks.com/topic/276243-php-foreach-video-in-file/#findComment-1421552 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? Link to comment https://forums.phpfreaks.com/topic/276243-php-foreach-video-in-file/#findComment-1421554 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). Link to comment https://forums.phpfreaks.com/topic/276243-php-foreach-video-in-file/#findComment-1421562 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> Link to comment https://forums.phpfreaks.com/topic/276243-php-foreach-video-in-file/#findComment-1421689 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. Link to comment https://forums.phpfreaks.com/topic/276243-php-foreach-video-in-file/#findComment-1421695 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' Link to comment https://forums.phpfreaks.com/topic/276243-php-foreach-video-in-file/#findComment-1421697 Share on other sites More sharing options...
requinix Posted March 28, 2013 Share Posted March 28, 2013 Yep. Link to comment https://forums.phpfreaks.com/topic/276243-php-foreach-video-in-file/#findComment-1421706 Share on other sites More sharing options...
JesseToxik Posted March 29, 2013 Author Share Posted March 29, 2013 Ok cool thanks for the help Link to comment https://forums.phpfreaks.com/topic/276243-php-foreach-video-in-file/#findComment-1421720 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.