mmdfan
mmdfan

Reputation: 346

How to get all pictures and videos from a specific folder with php?

I'm trying with following code: It seems to get the images but not the videos. Maybe the PATHINFO_EXTENSION can not be compared to string?

<?php
$files = glob("MyFolder/*.*");
for ($i = 0;$i < count($files);$i++) {
    $image = $files[$i];
    $supported_file = array(
        'jpg',
        'jpeg',
        'png',
        'mp4',
    );
    $ext = strtolower(pathinfo($image, PATHINFO_EXTENSION));
    if (in_array($ext, $supported_file)) {
        if (PATHINFO_EXTENSION == 'mp4') {
            echo '<video controls> <source src="' . $image . '" type="video/mp4"/>';
            echo '</video>';
        } else {
            echo '<img src="' . $image . '" alt="Random image" />';
        }
    } else {
        continue;
    }
}
?>

Upvotes: 3

Views: 330

Answers (1)

beercohol
beercohol

Reputation: 2587

You've got an error on your line that checks the extension against 'mp4':

if(PATHINFO_EXTENSION=='mp4')

You should be comparing against the value of your $ext variable, like so:

if($ext == 'mp4')

PATHINFO_EXTENSION is not your variable, it's just a parameter to the pathinfo() function which tells it to return the file extension for a given path.

PHP Docs - pathinfo: http://php.net/manual/en/function.pathinfo.php

The return value from the call to pathinfo() is stored in your variable $ext, and it's this that you need to compare against.

Upvotes: 1

Related Questions