Jump to content

[SOLVED] php foreach dir list files in dir


shocker-z

Recommended Posts

Hi,

 

I've got some code to loop through a directory and it's sub directories and bring back a list of files and folders. What im wanting to do is have this in a state where i can put all info into a database so i can quickly index and then see what files are in what folders. It's all my security camera triggerd avi files.

 

The following code will loop through an give me an array of files and folders

 

$dir = "E:/liam/camtest/cars/";
function dirlist($dir) {
    foreach(scandir($dir) as $entry)
        if($entry != '.' && $entry != '..')
        {
            $entry  = $dir.'/'.$entry;
            if(is_dir($entry))
            {
                $path = pathinfo($entry);
                $listarray[$path['basename']] = dirlist($entry);
            }
            else
            {
                $path = pathinfo($entry);
                $listarray[] = $path['basename'];
            }
        }
    return($listarray);
}
$array=dirlist($dir);



foreach ($array as $line) {
foreach ($line as $subline) {
	echo $subline.'<Br>';
}
}

 

And this works it outputs:

 

A-212718-212759-00720.avi
A-213001-213017-00384.avi
A-055819-055855-00894.avi
A-101026-101046-00498.avi
A-113550-113607-00431.avi

 

but im also needing the folder name as this is the date, how can i combine the 2 into the array to result in a full listing of foldername and associated filename?

 

 

Regards

Liam

Link to comment
https://forums.phpfreaks.com/topic/76989-solved-php-foreach-dir-list-files-in-dir/
Share on other sites

Solved using the following

 

<?php
include('connect.php');
$path = "E:/liam/camtest/cars/";
function ListFolder($path)
{
    $dir_handle = @opendir($path) or die("Unable to open $path");
    $dirname = end(explode("/", $path));
    while (false !== ($file = readdir($dir_handle)))
    {
        if($file!="." && $file!="..")
        {
            if (is_dir($path."/".$file))
            {
                ListFolder($path."/".$file);
            }
            else
            {
	     mysql_query("INSERT INTO videos (folder, file) VALUES('".$dirname."','".$file."')") or die('Error inserting files: '.mysql_error());

            }
        }
    }
    closedir($dir_handle);
}
ListFolder($path);
?>

 

regards

Liam

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.