note 87760 added to function.scandir
| From: | admin at utahfishinginfo dot com | Date: | Fri, 19 Dec 2008 23:47:24 +0000 |
| Subject: | note 87760 added to function.scandir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-148372@lists.php.net to get a copy of this message | ||
Here is a function that parses your website's directory tree, similar to the scanDirectories
function provided by fatpratmatt. However, this allows you to filter out all files except for those
with specific extensions, in addition to allowing you to ignore entire directories, and also has a
list of specific files to ignore. One last addition is that it allows you to use a regular
expression to ignore another set of files.
I use this to walk the directory structure of my website, and to generate a sitemap.xml for Google
sitemaps to index my site. This allows me to skip dev, data, library, etc. folders, and files that
are not front end files.
The way it's written means you don't need to worry about excluding specific files at all
unless they have the extension you are looking for (i.e. html or php files).
Enjoy.
<?php
function get_files($root_dir, $all_data=array())
{
// only include files with these extensions
$allow_extensions = array("php", "html");
// make any specific files you wish to be excluded
$ignore_files =
array("gdform.php","fishheader.php","fishfooter.php",
"sitelinks.php","google204accd1c3ac0501.html","sitemapxml.php",
"rotate.php", "fishstockingreport2.php", "repcentral.php",
"repne.php",
"repnorth.php","reppowell.php","repse.php","repsouth.php","repse.php",
"stockreplib.php","iestyles.php");
$ignore_regex = '/^_/';
// skip these directories
$ignore_dirs = array(".", "..", "images", "dev",
"lib", "data", "osh", "fiq", "google",
"stats", "_db_backups", "maps", "php_uploads",
"test");
// run through content of root directory
$dir_content = scandir($root_dir);
foreach($dir_content as $key => $content)
{
$path = $root_dir.'/'.$content;
if(is_file($path) && is_readable($path))
{
// skip ignored files
if(!in_array($content, $ignore_files))
{
if (preg_match($ignore_regex,$content) == 0)
{
$content_chunks = explode(".",$content);
$ext = $content_chunks[count($content_chunks) - 1];
// only include files with desired extensions
if (in_array($ext, $allow_extensions))
{
// save file name with path
$all_data[] = $path;
}
}
}
}
// if content is a directory and readable, add path and name
elseif(is_dir($path) && is_readable($path))
{
// skip any ignored dirs
if(!in_array($content, $ignore_dirs))
{
// recursive callback to open new directory
$all_data = get_files($path, $all_data);
}
}
} // end foreach
return $all_data;
} // end get_files()
?>
----
Server IP: 208.69.120.35
Probable Submitter: 24.16.9.247
----
Manual Page -- http://www.php.net/manual/en/function.scandir.php
Edit -- https://master.php.net/note/edit/87760
Del: integrated -- https://master.php.net/note/delete/87760/integrated
Del: useless -- https://master.php.net/note/delete/87760/useless
Del: bad code -- https://master.php.net/note/delete/87760/bad+code
Del: spam -- https://master.php.net/note/delete/87760/spam
Del: non-english -- https://master.php.net/note/delete/87760/non-english
Del: in docs -- https://master.php.net/note/delete/87760/in+docs
Del: other reasons-- https://master.php.net/note/delete/87760
Reject -- https://master.php.net/note/reject/87760
Search -- https://master.php.net/manage/user-notes.php