| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | define('FILE_ERROR_NO_SOURCE', 100); |
|---|
| 10 | define('FILE_ERROR_COPY_FAILED', 101); |
|---|
| 11 | define('FILE_ERROR_DST_DIR_FAILED', 102); |
|---|
| 12 | define('FILE_COPY_OK', 103); |
|---|
| 13 | define('FILE_ERROR_DST_DIR_EXIST', 104); |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | class Files |
|---|
| 23 | { |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | function copyFile($source, $destination_dir, $destination_file, $unique=true) |
|---|
| 37 | { |
|---|
| 38 | if(!is_uploaded_file($source) && !(file_exists($source) && is_file($source))) |
|---|
| 39 | return FILE_ERROR_NO_SOURCE; |
|---|
| 40 | |
|---|
| 41 | $destination_dir = Files::fixPath($destination_dir); |
|---|
| 42 | |
|---|
| 43 | if(!is_dir($destination_dir)) |
|---|
| 44 | Return FILE_ERROR_DST_DIR_FAILED; |
|---|
| 45 | |
|---|
| 46 | $filename = Files::escape($destination_file); |
|---|
| 47 | |
|---|
| 48 | if($unique) |
|---|
| 49 | { |
|---|
| 50 | $dotIndex = strrpos($destination_file, '.'); |
|---|
| 51 | $ext = ''; |
|---|
| 52 | if(is_int($dotIndex)) |
|---|
| 53 | { |
|---|
| 54 | $ext = substr($destination_file, $dotIndex); |
|---|
| 55 | $base = substr($destination_file, 0, $dotIndex); |
|---|
| 56 | } |
|---|
| 57 | $counter = 0; |
|---|
| 58 | while(is_file($destination_dir.$filename)) |
|---|
| 59 | { |
|---|
| 60 | $counter++; |
|---|
| 61 | $filename = $base.'_'.$counter.$ext; |
|---|
| 62 | } |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | if (!copy($source, $destination_dir.$filename)) |
|---|
| 66 | return FILE_ERROR_COPY_FAILED; |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | if (is_file($destination_dir.$filename)) |
|---|
| 70 | Return $filename; |
|---|
| 71 | else |
|---|
| 72 | return FILE_ERROR_COPY_FAILED; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | function createFolder($newFolder) |
|---|
| 81 | { |
|---|
| 82 | mkdir ($newFolder, 0777); |
|---|
| 83 | return chmod($newFolder, 0777); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | function escape($filename) |
|---|
| 94 | { |
|---|
| 95 | Return preg_replace('/[^\w\._]/', '_', $filename); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | function delFile($file) |
|---|
| 104 | { |
|---|
| 105 | if(is_file($file)) |
|---|
| 106 | Return unlink($file); |
|---|
| 107 | else |
|---|
| 108 | Return false; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | function delFolder($folder, $recursive=false) |
|---|
| 120 | { |
|---|
| 121 | $deleted = true; |
|---|
| 122 | if($recursive) |
|---|
| 123 | { |
|---|
| 124 | $d = dir($folder); |
|---|
| 125 | while (false !== ($entry = $d->read())) |
|---|
| 126 | { |
|---|
| 127 | if ($entry != '.' && $entry != '..') |
|---|
| 128 | { |
|---|
| 129 | $obj = Files::fixPath($folder).$entry; |
|---|
| 130 | |
|---|
| 131 | if (is_file($obj)) |
|---|
| 132 | { |
|---|
| 133 | $deleted &= Files::delFile($obj); |
|---|
| 134 | } |
|---|
| 135 | else if(is_dir($obj)) |
|---|
| 136 | { |
|---|
| 137 | $deleted &= Files::delFolder($obj, $recursive); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | } |
|---|
| 141 | } |
|---|
| 142 | $d->close(); |
|---|
| 143 | |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | if(is_dir($folder)) |
|---|
| 149 | $deleted &= rmdir($folder); |
|---|
| 150 | else |
|---|
| 151 | $deleted &= false; |
|---|
| 152 | |
|---|
| 153 | Return $deleted; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | function fixPath($path) |
|---|
| 162 | { |
|---|
| 163 | |
|---|
| 164 | if(!(substr($path,-1) == '/')) |
|---|
| 165 | $path .= '/'; |
|---|
| 166 | Return $path; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | function makePath($pathA, $pathB) |
|---|
| 176 | { |
|---|
| 177 | $pathA = Files::fixPath($pathA); |
|---|
| 178 | if(substr($pathB,0,1)=='/') |
|---|
| 179 | $pathB = substr($pathB,1); |
|---|
| 180 | Return Files::fixPath($pathA.$pathB); |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | function makeFile($pathA, $pathB) |
|---|
| 191 | { |
|---|
| 192 | $pathA = Files::fixPath($pathA); |
|---|
| 193 | if(substr($pathB,0,1)=='/') |
|---|
| 194 | $pathB = substr($pathB,1); |
|---|
| 195 | |
|---|
| 196 | Return $pathA.$pathB; |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | |
|---|
| 205 | function formatSize($size) |
|---|
| 206 | { |
|---|
| 207 | if($size < 1024) |
|---|
| 208 | return $size.' bytes'; |
|---|
| 209 | else if($size >= 1024 && $size < 1024*1024) |
|---|
| 210 | return sprintf('%01.2f',$size/1024.0).' KB'; |
|---|
| 211 | else |
|---|
| 212 | return sprintf('%01.2f',$size/(1024.0*1024)).' MB'; |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | function dirSize($dirName = '.') |
|---|
| 222 | { |
|---|
| 223 | $dir = dir($dirName); |
|---|
| 224 | $size = 0; |
|---|
| 225 | |
|---|
| 226 | while ($file = $dir->read()) { |
|---|
| 227 | if ($file != '.' && $file != '..') |
|---|
| 228 | { |
|---|
| 229 | if (is_dir("$dirName$file")) |
|---|
| 230 | { |
|---|
| 231 | $size += Files::dirSize($dirName . '/' . $file); |
|---|
| 232 | } |
|---|
| 233 | else |
|---|
| 234 | { |
|---|
| 235 | $size += filesize($dirName . '/' . $file); |
|---|
| 236 | } |
|---|
| 237 | } |
|---|
| 238 | } |
|---|
| 239 | $dir->close(); |
|---|
| 240 | return $size; |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | function renameFile($oldPath, $newName) { |
|---|
| 250 | |
|---|
| 251 | if(!(file_exists($oldPath) && is_file($oldPath))) |
|---|
| 252 | return FILE_ERROR_NO_SOURCE; |
|---|
| 253 | |
|---|
| 254 | $oldFileParts = pathinfo($oldPath); |
|---|
| 255 | |
|---|
| 256 | $newPath = $oldFileParts['dirname'] . '/' |
|---|
| 257 | . $newName |
|---|
| 258 | . (!empty($oldFileParts['extension']) ? '.' . $oldFileParts['extension'] : ''); |
|---|
| 259 | |
|---|
| 260 | if (file_exists($newPath)) |
|---|
| 261 | return false; |
|---|
| 262 | |
|---|
| 263 | if (!rename($oldPath, $newPath)) |
|---|
| 264 | return FILE_ERROR_COPY_FAILED; |
|---|
| 265 | |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | function rename ($oldPath,$newPath) |
|---|
| 269 | { |
|---|
| 270 | if(!(is_dir($oldPath) || is_file($oldPath))) |
|---|
| 271 | return FILE_ERROR_NO_SOURCE; |
|---|
| 272 | |
|---|
| 273 | if (file_exists($newPath)) |
|---|
| 274 | return FILE_ERROR_DST_DIR_EXIST; |
|---|
| 275 | |
|---|
| 276 | $ret = rename($oldPath, $newPath); |
|---|
| 277 | if (!$ret) |
|---|
| 278 | return FILE_ERROR_COPY_FAILED; |
|---|
| 279 | else return FILE_COPY_OK; |
|---|
| 280 | } |
|---|
| 281 | |
|---|
| 282 | |
|---|
| 283 | |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | |
|---|
| 287 | |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | function copyDir($basePath, $source, $dest, $overwrite = false) |
|---|
| 294 | { |
|---|
| 295 | if(!is_dir($basePath . $dest)) |
|---|
| 296 | { |
|---|
| 297 | if (!@mkdir($basePath . $dest)) return FILE_ERROR_DST_DIR_FAILED; |
|---|
| 298 | } |
|---|
| 299 | if($handle = opendir($basePath . $source)) |
|---|
| 300 | { |
|---|
| 301 | while( ($file = readdir($handle)) !== false) |
|---|
| 302 | { |
|---|
| 303 | if($file != '.' && $file != '..') |
|---|
| 304 | { |
|---|
| 305 | $path = $source . '/' . $file; |
|---|
| 306 | if(is_file($basePath . $path)) |
|---|
| 307 | { |
|---|
| 308 | |
|---|
| 309 | |
|---|
| 310 | |
|---|
| 311 | |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | Files::copyFile($basePath . $path, $basePath . $dest . '/', $file, true); |
|---|
| 316 | } |
|---|
| 317 | elseif(is_dir($basePath . $path)) |
|---|
| 318 | { |
|---|
| 319 | if(!is_dir($basePath . $dest . '/' . $file)) |
|---|
| 320 | { |
|---|
| 321 | mkdir($basePath . $dest . '/' . $file); |
|---|
| 322 | Files::copyDir($basePath, $path, $dest . '/' . $file, $overwrite); |
|---|
| 323 | } |
|---|
| 324 | } |
|---|
| 325 | } |
|---|
| 326 | } |
|---|
| 327 | closedir($handle); |
|---|
| 328 | } |
|---|
| 329 | return true; |
|---|
| 330 | } |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | ?> |
|---|