[PHP] Create Filelist of files with Specific Extensions

Status
Not open for further replies.

viruz99

Active Member
1,547
2010
96
0
Hey Guys ,

Any idea how to create a filelist of a location via php with files with a specific extension :

E.g : .avi

let's say the location is "/location/folder1/"

Inside i have : Folderx , folderz , file1.avi , file2.avi , file2.rar

Now i want to create a filelist of that folder using only files with .avi ext .





 
2 comments
PHP:
function read_dir($dir) {
    $handle = opendir ($dir);
    while (false !== ($f = readdir ($handle))){
        if ($f != "."){
            if ($f != ".."){
                $file=$dir.$f;
                if(is_file($file)) {
                    $ext = strtolower(substr($file, strrpos($file, '.') + 1));
                    if($ext == 'avi') echo $file.'<br />';
                
                }
            }
        }
    }
}

read_dir('full_path_to_dir');
 
PHP:
<?php

$folder = array(); // Creates a array for use later

foreach (glob("/location/folder1/*.avi") as $thefolder) { // Use for each to go through and get each folder & file in the given directory
    If (is_file($thefolder)) { // We only want to get files so we are making sure that we are adding a file and not a directory.
        $folder[] = $thefolder; // Adds the file to the array created
    }
}

print_r($folder); // Print out the list for debug

?>
You can specify the filename search pattern in glob
 
Status
Not open for further replies.
Back
Top