Cercare un file in una directory con php

Posted on 11. Nov, 2008 by Administrator in files, php, script php, tutorials


< ?php
// find files below a given directory whose name matches a given patterm.
// For example, to get an array of all files under /htdocs matching the
// pattern *.content.php use a call such as:
//
// $page_list = find_file("/htdocs", ".content.php$");

function find_file($dir, $pattern, $found = false) {

// find files matching $pattern

if (! $d = @dir($dir))
return false;

while($file = $d -> read()) {

// ignore . and ..

if (ereg("^[.]+$", $file))
continue;

// if the file is a regular file and matches the pattern, add it to
// the $found[] array

if (ereg($pattern, "${dir}/$file" && is_file(${dir}/$file)))
$found[] = " $dir/$file";

// recurse down through directories, adding found files as we go

if (is_dir("${dir}/$file"))
$found = array_expand(find_file("${dir}/$file", $pattern,
$found), $found);
}

return $found;
}

// I've included my array_expand() function (available
// elsewhere on Zend) for completeness.

function array_expand($base = 0, $extra = 0) {

// Take two one-dimensional array, step through the first looking for
// each element of the second. If any of the elements in the second
// array are not in the first, append them. Kind of an extended version
// of PHP4's array_merge() function

if (!is_array($base))
return false;

$ext_arr = (is_array($extra)) ? $extra : array($extra);

for ($i = 0; $i < sizeof($ext_arr); $i++) {

if (!in_array($ext_arr[$i], $base))
$base[] = $ext_arr[$i];

}
return $base;
}
?>


Correlati

Leave a reply