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

Link to comment
Share on other sites

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.