I am trying to create an a list of files within a directory, including those in subdirectories, showing their full path, filename and extension. Any guidance would be appreciated.
After plenty of reading I have attempted several things, this is the most recent I found and tried but have not had any joy, for now I am just looking for the browser to display the above information.
$dir = '../test/images/';
function getFileList($dir, $recurse=false)
{
// array to hold return value
$retval = array();
// add trailing slash if missing
if(substr($dir, -1) != "/") $dir .= "/";
// open pointer to directory and read list of files
$d = @dir($dir) or die("getFileList: Failed opening directory $dir for reading");
while(false !== ($entry = $d->read())) {
// skip hidden files
if($entry[0] == ".") continue;
if(is_dir("$dir$entry")) {
$retval[] = array(
"name" => "$dir$entry/",
"type" => filetype("$dir$entry"),
"size" => 0,
"lastmod" => filemtime("$dir$entry")
);
if($recurse && is_readable("$dir$entry/")) {
$retval = array_merge($retval, getFileList("$dir$entry/", true));
}
} elseif(is_readable("$dir$entry")) {
$retval[] = array(
"name" => "$dir$entry",
"type" => mime_content_type("$dir$entry"),
"size" => filesize("$dir$entry"),
"lastmod" => filemtime("$dir$entry")
);
}
}
$d->close();
return $retval;
}
print_r($retval);
My ultimate goal is to create some php that on execute will look in a directory, create the subdirectory (and any subdirectories there in) with the same name in a new directory (so /test/album/ would be created as /live/album/). Then resize any images that are contained in each original directory and save them to their related new directory. I appreciate this is a lot and will no doubt have many questions as I proceed, but will save those for later.
Here’s a similar script I wrote for my sites: