http://php.net/manual/ro/class.recursivedirectoryiterator.php
$dir = getcwd();
$dir_iterator = new RecursiveDirectoryIterator($dir);
$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $file) {
$path = $file;
$str = file_get_contents($file);
$str = preg_replace($patterns, $replacements, $str);
file_put_contents($path, $str);
}
/*echo '<pre>';
var_dump($files);
echo '</pre>';*/
Am testat doar daca returneaza fisierele.
Nu am stat sa analizez codul lui birkoff, dar pare sa aiba lipsa functia read_info_dir, situatie in care va trebui sa ai $depth = false si iti va cauta doar in directorul in care specifici, nu va intra in subdirectoare.
function get_list_dir($path, $depth = false, $type = 'all', $inc = true, $exclude = array()) {
// Set list
$list = array();
// directory element is determined depending on the operating system
$elm = ( stristr(PHP_OS, 'win') === false ) ? '/' : '\\';
// memorizes the current path
$base_path = getcwd();
// change to the path specified
$is_changed = chdir($path);
if ( ! $is_changed )
return false;
$required_path = getcwd();
if ( ! $required_path )
return false;
// read path required
$director = opendir($required_path);
if ( ! $director ) {
// return to the base path
chdir($base_path);
return false;
}
// reads the current directory
$read = readdir($director);
if ( $read === false ) {
// return to the base path
chdir($base_path);
return false;
}
while ($read) {
// excluding files / directories unwanted
if (! in_array($read, $exclude) ) {
// check what type is required
switch($type) {
default:
case 'all': // returns all files and directories found
// to memorize what is currently
$list[] = ( $inc ) ? $required_path.$elm.$read : $read;
// if is director and requires completion
if ( is_dir($read) && $depth ) {
// browse the directory
$list[] = read_info_dir($read, $depth, $type, $inc, $exclude, $max);
}
break;
case 'dir': // only returns the list of directories found
// if is director
if ( is_dir($read) ) {
// to memorize what is currently
$list[] = ( $inc ) ? $required_path.$elm.$read : $read;
// if requires completion
if ( $depth ) {
// browse the directory
$list[] = read_info_dir($read, $depth, $type, $inc, $exclude, $max);
}
}
break;
case 'file': // only returns the list of files found
// check if file
if ( is_file($read) ) {
// to memorize what is currently
$list[] = ( $inc ) ? $required_path.$elm.$read : $read;
}
// else if is folder and it requires completion
elseif ( $depth ) {
// browse the directory
$list[] = read_info_dir($read, $depth, $type, $inc, $exclude, $max);
}
break;
} // end switch
} // end exclude
// go to next
$read = readdir($director);
} // end while
// director closes
closedir($director);
// returns to the initial path
chdir($base_path);
// return
return $list;
}
$excludem = array('.', '..', 'index.htm', 'index.html', '.htaccess');
$lista = get_list_dir(getcwd(), false, 'file', false, $excludem);
// afisam rezultatele gasite
if ( is_array($lista) && count($lista) )
foreach($lista as $file_name)
//echo $file_name;
$data = file_get_contents($file_name);
$data = preg_replace($patterns, $replacements, $data);
file_put_contents($file_name, $data);
else
echo 'nu a fost gasit nici un fisier in directorul specificat';
Am testat doar daca returneaza fisierele nu si daca le modifica, acolo modifici si tu.