public
Last active

General purpose folder and file processing for copy, move, delete, and size calculation

  • Download Gist
Files.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
<?php
/**
* File class
*
* @package Molajo
* @copyright 2013 Amy Stephen. All rights reserved.
* @license MIT, GPL v2 or later
*/
namespace Molajo;
 
use RecursiveIteratorIterator;
use RecursiveDirectoryIterator;
 
/**
* General purpose methods for processing a single file or set of folders and files
*
* Thanks http://Twitter.com/CoreyHaines for your feedback and assistance! https://gist.github.com/4677586
*
* 1. Copy or Move a file or folders/files:
*
* $files = new Files();
* $results = $files->copyOrMove('source/folder/optional-file',
* 'target/path', 'target-file-name-for-single-file.only', 'copy');
*
* 2. Delete a single file or all files and folders in a path:
*
* $files = new Files();
* $results = $files->delete('source/folder/optional-file.name');
*
* 3. Calculate the size of a single file or a set of files in a set of folders:
*
* $files = new Files();
* $results = $files->calculateSize('source/folder/optional-file.name');
*
*/
class Files
{
protected $path;
protected $options;
protected $filesystem;
protected $directories;
protected $files;
 
/**
* Get the file size of a given file or for the aggregate number of bytes in all directory files
*
* Recursive handling of files - uses file arrays created in Discovery
*
* @param string $path
*
* @return int
* @since 1.0
*/
public function calculateSize($path)
{
$size = 0;
 
$this->discovery($path);
 
foreach ($this->files as $file) {
$size = $size + filesize($file);
}
 
return $size;
}
 
/**
* Recursive Copy or Delete - uses Directory and File arrays created in Discovery
*
* Copy uses Directory array first to create folders, then copies the files
*
* @param string $path
* @param string $target
* @param string $target_name
* @param string $copyOrMove
*
* @return void
* @since 1.0
*/
function copyOrMove($path, $target, $target_name = '', $copyOrMove = 'copy')
{
if (file_exists($path)) {
} else {
return;
}
 
if (file_exists($target)) {
} else {
return;
}
 
$new_path = $target . '/' . $target_name;
 
$this->discovery($path);
 
if (count($this->directories) > 0) {
asort($this->directories);
foreach ($this->directories as $directory) {
 
$new_directory = $new_path . '/' . substr($directory, strlen($path), 99999);
 
if (basename($new_directory) == '.' || basename($new_directory) == '..') {
 
} elseif (file_exists($new_directory)) {
 
} else {
mkdir($new_directory);
}
}
}
 
if (count($this->files) > 0) {
foreach ($this->files as $file) {
$new_file = $new_path . '/' . substr($file, strlen($path), 99999);
\copy($file, $new_file);
}
}
 
if ($copyOrMove == 'move') {
 
if (count($this->files) > 0) {
foreach ($this->files as $file) {
unlink($file);
}
}
 
if (count($this->directories) > 0) {
arsort($this->directories);
foreach ($this->directories as $directory) {
if (basename($directory) == '.' || basename($directory) == '..') {
} else {
rmdir($directory);
}
}
}
}
 
return;
}
 
/**
* Recursive Delete, uses discovery Directory and File arrays to first delete files
* and then remove the folders
*
* @param $path
*
* @return int
* @since 1.0
*/
function delete($path)
{
if (file_exists($path)) {
} else {
return;
}
 
$this->discovery($path);
 
if (count($this->files) > 0) {
foreach ($this->files as $file) {
unlink($file);
}
}
 
if (count($this->directories) > 0) {
 
arsort($this->directories);
 
foreach ($this->directories as $directory) {
 
if (basename($directory) == '.' || basename($directory) == '..') {
} else {
rmdir($directory);
}
}
}
 
return;
}
 
/**
* Discovery retrieves folder and file names, useful for other file/folder operations
*
* @param $path
*
* @return void
* @since 1.0
*/
public function discovery($path)
{
$this->directories = array();
$this->files = array();
 
if (is_file($path)) {
$this->files[] = $path;
return;
}
 
if (is_dir($path)) {
} else {
return;
}
 
$this->directories[] = $path;
 
$objects = new RecursiveIteratorIterator (
new RecursiveDirectoryIterator($path),
RecursiveIteratorIterator::SELF_FIRST);
 
foreach ($objects as $name => $object) {
 
if (is_file($name)) {
$this->files[] = $name;
 
} elseif (is_dir($name)) {
 
if (basename($name) == '.' || basename($name) == '..') {
} else {
$this->directories[] = $name;
}
}
}
 
return;
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.