4 if (php_sapi_name() != 'cli' && !getenv('PHP_IS_CLI')) {
5 echo 'Script cannot be called from web-browser (if you are calling via cli,
6 set environment variable PHP_IS_CLI to work around this).';
12 * Filesystem tools not provided by default; can recursively create, copy
13 * and delete folders. Some template methods are provided for extensibility.
14 * @note This class must be instantiated to be used, although it does
21 * Recursively creates a directory
22 * @param string $folder Name of folder to create
23 * @note Adapted from the PHP manual comment 76612
25 function mkdir($folder) {
26 $folders = preg_split("#[\\\\/]#", $folder);
28 for($i = 0, $c = count($folders); $i < $c; $i++
) {
29 if(empty($folders[$i])) {
31 // special case for root level
32 $base .= DIRECTORY_SEPARATOR
;
36 $base .= $folders[$i];
40 $base .= DIRECTORY_SEPARATOR
;
45 * Copy a file, or recursively copy a folder and its contents; modified
46 * so that copied files, if PHP, have includes removed
48 * @author Aidan Lister <aidan@php.net>
49 * @version 1.0.1-modified
50 * @link http://aidanlister.com/repos/v/function.copyr.php
51 * @param string $source Source path
52 * @param string $dest Destination path
53 * @return bool Returns TRUE on success, FALSE on failure
55 function copyr($source, $dest) {
56 // Simple copy for a file
57 if (is_file($source)) {
58 return $this->copy($source, $dest);
60 // Make destination directory
64 // Loop through the folder
66 while (false !== $entry = $dir->read()) {
68 if ($entry == '.' ||
$entry == '..') {
71 if (!$this->copyable($entry)) {
74 // Deep copy directories
75 if ($dest !== "$source/$entry") {
76 $this->copyr("$source/$entry", "$dest/$entry");
85 * Stub for PHP's built-in copy function, can be used to overload
88 function copy($source, $dest) {
89 return copy($source, $dest);
93 * Overloadable function that tests a filename for copyability. By
94 * default, everything should be copied; you can restrict things to
95 * ignore hidden files, unreadable files, etc.
97 function copyable($file) {
102 * Delete a file, or a folder and its contents
104 * @author Aidan Lister <aidan@php.net>
106 * @link http://aidanlister.com/repos/v/function.rmdirr.php
107 * @param string $dirname Directory to delete
108 * @return bool Returns TRUE on success, FALSE on failure
110 function rmdirr($dirname)
113 if (!file_exists($dirname)) {
117 // Simple delete for a file
118 if (is_file($dirname) ||
is_link($dirname)) {
119 return unlink($dirname);
122 // Loop through the folder
123 $dir = dir($dirname);
124 while (false !== $entry = $dir->read()) {
126 if ($entry == '.' ||
$entry == '..') {
130 $this->rmdirr($dirname . DIRECTORY_SEPARATOR
. $entry);
135 return rmdir($dirname);