Reply to The most craziest function for recursive handling with files
The most craziest function for recursive handling with files Posted by Alexander Shatalov But it works And I adore that)PHP function FuncDirFileRecursive($dir, $to, $func_file = 'copy', $func_dir = 'mkdir', $total = 0, $time = 0, $chmod = 0777, $str = 0, $strto = 0) { if (!$dir) return 0; $dir = File::fixPath($dir); if ($to) $to = File::fixPath($to); if ($to && strlen($to)<=2) return 0; if (!is_dir($dir)) { return 0; } $dh = @opendir($dir); if (!$dh) { return 0; } $func_file = trim(strtolower($func_file)); if (($func_dir==='mkdir' || $func_dir==='rename') && $to && !is_dir($to)) File::checkDir($to); $t = time(); while ($f = readdir($dh)) { if ($f=='..' || $f=='.') continue; if ($time && filemtime($dir.$file) > $t-$time) continue; if (is_dir($dir.$f) && $func_dir!==NULL) { if ($func_dir) { if ($func_dir==='rmdir') { if (!is_dir($dir.$f) && @rmdir($dir.$f)) { $total++; } } elseif (function_exists($func_dir) && !is_dir($to.$f)) { if ($func_dir($to.$f,0777)) { File::chown2($to.$f,File::CHMOD_READ | File::CHMOD_WRITE); $total++; } } } if ($func_file==='array') { $total = FuncDirFileRecursive($dir.$f, $to.$f, $func_file, $func_dir, $total, $time, $chmod, $str, $strto); } else { $total += FuncDirFileRecursive($dir.$f, $to.$f, $func_file, $func_dir, 0, $time, $chmod, $str, $strto); } } elseif (is_file($dir.$f) && $func_file) { if ($func_file==='array') { $total[$dir][] = $f; } elseif ($func_file==='filesize') { $total += filesize($dir.$f); } elseif ($func_file==='rename') { if (is_file($to.$f)) unlink($to.$f); $total += rename($dir.$f,$to.$f); } elseif ($func_file==='copy') { if (is_file($to.$f)) unlink($to.$f); $total += copy($dir.$f,$to.$f); } elseif ($func_file=='copy_replace' && $str && $strto) { if (copyReplace($dir.$f,$to.$f,$str,$strto)) { $total++; } } elseif ($func_file==='unlink') { File::chown2($dir.$f,File::CHMOD_READ | File::CHMOD_WRITE); if (@unlink($dir.$f)) { $total++; } } else { if (function_exists($func_file) && $func_file($dir.$f,$to.$f)) { $total++; } if ($chmod) @chmod($to.$f,$chmod); } } } closedir($dh); if (!is_array($total)) $total = (int)$total; return $total; } |