Skip to content

Instantly share code, notes, and snippets.

@joseluisq
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joseluisq/6d92221e0e2fb1153204 to your computer and use it in GitHub Desktop.
Save joseluisq/6d92221e0e2fb1153204 to your computer and use it in GitHub Desktop.
PHP Directory handler class
<?php
/**
* @author Jose Luis Quintana <joseluisquintana.pe>
*/
class JDirectory {
private $_ignore_list = array();
function ignore_list($ignore_list) {
$this->_ignore_list = $ignore_list;
}
function remove_dir($dir, $remove_me = TRUE) {
if (!$dh = @opendir($dir)) {
return;
}
while (($file = @readdir($dh)) != FALSE) {
if ($file == '.' || $file == '..') {
continue;
}
if (is_dir($dir . '/' . $file)) {
if (!in_array($dir . '/' . $file, $this->_ignore_list)) {
$this->remove_dir($dir . '/' . $file, $this->_ignore_list);
@rmdir($dir . '/' . $file);
}
} else {
if (!in_array($dir . '/' . $file, $this->_ignore_list)) {
@unlink($dir . '/' . $file);
}
}
}
if ($remove_me) {
@rmdir($dir);
}
closedir($dh);
}
function copy_dir($src, $dst) {
$dir = opendir($src);
@mkdir($dst);
while (false !== ( $file = readdir($dir))) {
if (( $file != '.' ) && ( $file != '..' )) {
if (is_dir($src . '/' . $file)) {
$this->copy_dir($src . '/' . $file, $dst . '/' . $file);
} else {
copy($src . '/' . $file, $dst . '/' . $file);
}
}
}
closedir($dir);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment