Jump to content

Recommended Posts

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

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>';
}

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 by JesseToxik

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.