├── .gitignore ├── .travis.yml ├── composer.json ├── example ├── index2.php └── index.php ├── LICENSE ├── README.md └── src └── Files.php /.gitignore: -------------------------------------------------------------------------------- 1 | vendor/ 2 | composer.lock 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 7.0 5 | - 7.1 6 | - 7.2 7 | 8 | install: 9 | - travis_retry composer install --no-interaction 10 | 11 | before_script: 12 | - composer update 13 | 14 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "lablnet/files", 3 | "description": "PHP Files package.", 4 | "keywords": ["php", "files","psr-3","free","package","Lablnet"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Malik Umer Farooq", 9 | "email":"lablnet01@gmail.com", 10 | "homepage": "https://softhub99.com" 11 | } 12 | ], 13 | "autoload": { 14 | "psr-4": { 15 | "Lablnet\\": "src/" 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /example/index2.php: -------------------------------------------------------------------------------- 1 | filesUpload($_FILES['file'], '/', 'image', count($_FILES['file']['name'])); 10 | foreach ($status as $key => $value) { 11 | var_dump($value); 12 | } 13 | } 14 | ?> 15 | 16 | 17 | 18 | PHP File Manipulation Class Examples 19 | 20 | 21 |
22 | File Upload 23 |
24 | 25 | 26 |
27 | 28 |
29 |
30 | 31 | 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Malik Umer Farooq 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /example/index.php: -------------------------------------------------------------------------------- 1 | open('test.txt', 'writeOnly')->write('I am test files'); 12 | 13 | // read the file 14 | var_dump($files->open('test.txt', 'readOnly')->read('test.txt')); 15 | 16 | //delete the file 17 | $files->delete('test.txt'); 18 | 19 | //Make dir 20 | $files->mkDir('name'); 21 | 22 | //Change premission 23 | $files->permission('test.txt', 0774); 24 | 25 | //Delete files 26 | $files->deleteFiles(['test.txt']); 27 | 28 | //Copy files 29 | $files->copyFiles('/name', 'dir/', ['test.txt']); 30 | 31 | //Move files 32 | $files->moveFiles('/', 'dir/', ['test.txt']); 33 | 34 | //Delete dirs 35 | $files->deleteDirs(['test.txt']); 36 | 37 | //Copy dirs 38 | $files->copyDirs('/', 'dir/', ['test.txt']); 39 | 40 | //Move dirs 41 | $files->moveDirs('/', 'dir/', ['test.txt']); 42 | 43 | //File upload 44 | $status = $files->fileUpload($_FILES['file'], '/', 'image'); 45 | var_dump($status); 46 | 47 | //Multiple file upload 48 | $status = $files->filesUpload($_FILES['file'], '/', 'image', count($_FILES['file']['name'])); 49 | var_dump($status); 50 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PHP Files Class 2 | This package can manipulate files and directories in several ways. 3 | 4 | 5 | ## This package can manipulate files and directories in several ways. 6 | 7 | It can perform several types of operations. Currently it can: 8 | 9 | 1. Create directory 10 | 2. Generate random string 11 | 3. Change file permission 12 | 4. Copy Files or folders 13 | 5. Move files and folders 14 | 6. Delete files and folders 15 | 7. Upload files with validation 16 | 8. Multiple file upload with validation 17 | 9. read/write files 18 | ## Requirement 19 | 20 | - PHP 21 | - Composer 22 | 23 | ## install 24 | run this command 25 | ``` composer require lablnet/files``` 26 | 27 | ## usage 28 | 29 | ```php 30 | open('test.txt','writeOnly')->write("I am test files"); 40 | 41 | // read the file 42 | var_dump($files->open('test.txt','readOnly')->read('test.txt')); 43 | 44 | //delete the file 45 | $files->delete('test.txt'); 46 | 47 | 48 | //Make dir 49 | $files->mkDir('name'); 50 | 51 | //Change premission 52 | $files->permission('test.txt',0774); 53 | 54 | //Delete files 55 | $files->deleteFiles(['test.txt']); 56 | 57 | //Copy files 58 | $files->copyFiles('/name','dir/',['test.txt']); 59 | 60 | //Move files 61 | $files->moveFiles('/','dir/',['test.txt']); 62 | 63 | 64 | //Delete dirs 65 | $files->deleteDirs(['test.txt']); 66 | 67 | //Copy dirs 68 | $files->copyDirs('/','dir/',['test.txt']); 69 | 70 | //Move dirs 71 | $files->moveDirs('/','dir/',['test.txt']); 72 | 73 | //File upload 74 | $status = $files->fileUpload($_FILES['file'],'/','image'); 75 | var_dump($status); 76 | 77 | //Multiple file upload 78 | $status = $files->filesUpload($_FILES['file'],'/','image',count($_FILES['file']['name'])); 79 | var_dump($status); 80 | 81 | 82 | ``` -------------------------------------------------------------------------------- /src/Files.php: -------------------------------------------------------------------------------- 1 | 7 | * @author-profile https://www.facebook.com/malikumerfarooq01/ 8 | * 9 | * For the full copyright and license information, please view the LICENSE 10 | * file that was distributed with this source code. 11 | * 12 | * @license MIT 13 | */ 14 | 15 | namespace Lablnet; 16 | 17 | class Files 18 | { 19 | /* 20 | * The default value for recursive create dirs 21 | */ 22 | private $recursiveDirectories = true; 23 | 24 | /* 25 | * Default value for chmod on create directory 26 | */ 27 | private $defCHMOD = 0755; 28 | 29 | /* 30 | * Mine types of file 31 | */ 32 | private $mineTypes = [ 33 | 'application/x-zip-compressed', 34 | 'application/msword', 35 | 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 36 | 'image/gif', 37 | 'image/jpeg', 38 | 'image/jpeg', 39 | 'audio/mpeg', 40 | 'video/mp4', 41 | 'application/pdf', 42 | 'image/png', 43 | 'application/zip', 44 | 'application/et-stream', 45 | 'image/x-icon', 46 | 'image/icon', 47 | 'image/svg+xml', 48 | ]; 49 | /* 50 | * Types 51 | */ 52 | private $types = [ 53 | 'image' => ['jpg', 'png', 'jpeg', 'gif', 'ico', 'svg'], 54 | 'zip' => ['zip', 'tar', '7zip', 'rar'], 55 | 'docs' => ['pdf', 'docs', 'docx'], 56 | 'media' => ['mp4', 'mp3', 'wav', '3gp'], 57 | ]; 58 | 59 | /* 60 | * resource 61 | */ 62 | private $resource; 63 | /* 64 | * Mode of files 65 | */ 66 | private $modes = [ 67 | 'readOnly' => 'r', 68 | 'readWrite' => 'r+', 69 | 'writeOnly' => 'w', 70 | 'writeMaster' => 'w+', 71 | 'writeAppend' => 'a', 72 | 'readWriteAppend' => 'a+', 73 | ]; 74 | 75 | /** 76 | * Define the recursive create directories. 77 | * 78 | * @param $value recursive status true|false. 79 | * 80 | * @return current value 81 | */ 82 | public function recursiveCreateDir($value = null) 83 | { 84 | if ($value === null) { 85 | return $this->recursiveDirectories; 86 | } else { 87 | $this->recursiveDirectories = $value; 88 | } 89 | } 90 | 91 | /** 92 | * Define the CHMOD for created dir. 93 | * 94 | * @param (string) $value CHMOD value default: 0755. 95 | * 96 | * @return current value 97 | */ 98 | public function defaultCHMOD($value = null) 99 | { 100 | if ($value === null) { 101 | return $this->defCHMOD; 102 | } else { 103 | $this->defCHMOD = $value; 104 | } 105 | } 106 | 107 | /** 108 | * Add the mine type. 109 | * 110 | * @param (string) $type correct mine type. 111 | * 112 | * @return void 113 | */ 114 | public function addMineTypes($type) 115 | { 116 | array_push($this->mineTypes, $type); 117 | } 118 | 119 | /** 120 | * Add the extemsio. 121 | * 122 | * @param (string) $type Correct type. 123 | * @param (strubg) $sub Extensions 124 | * 125 | * @return void 126 | */ 127 | public function addExt($type, $ext) 128 | { 129 | array_push($this->types[$type], $ext); 130 | } 131 | 132 | /** 133 | * Make the dir. 134 | * 135 | * @param (string) $name Name of dir with path. 136 | * @param (string) $recursive Recursive mode create: null|true|false. 137 | * @param (string) $chmod Directory permission on create: 0755 138 | * 139 | * @return bool 140 | */ 141 | public function mkDir($name, $recursive = null, $chmod = null) 142 | { 143 | // test the recursive mode with default value 144 | $recursive = ($recursive === null) ? $this->recursiveDirectories : $recursive; 145 | // test the chmod with default value 146 | $chmod = ($chmod === null) ? $this->defCHMOD : $chmod; 147 | if (!is_dir($name)) { 148 | // this change to permit create dir in recursive mode 149 | return (mkdir($name, $chmod, $recursive)) ? true : false; 150 | } 151 | 152 | return false; 153 | } 154 | 155 | /** 156 | * Make the permission. 157 | * 158 | * @param (string) $source Name of file or directory with path. 159 | * @param (int) $pre Valid premission 160 | * 161 | * @return bool 162 | */ 163 | public function permission($source, $pre) 164 | { 165 | if (!is_dir($name)) { 166 | return (file_exists($source)) ? chmod($source, $pre) : false; 167 | } 168 | 169 | return false; 170 | } 171 | 172 | /** 173 | * Copy files. 174 | * 175 | * @param (string) $source name of file or directory with path. 176 | * @param (string) $target Target directory 177 | * @param (array) $files Files to be copy 178 | * 179 | * @return void 180 | */ 181 | public function copyFiles($source, $target, $files) 182 | { 183 | $this->mkDir($target); 184 | foreach ($files as $file => $value) { 185 | if (file_exists($source.$value)) { 186 | copy($source.$value, $target.$value); 187 | } 188 | } 189 | } 190 | 191 | /** 192 | * Move files. 193 | * 194 | * @param (string) $source Name of file or directory with path. 195 | * @param (string) $target Target directory 196 | * @param (array) $files Files to be move 197 | * 198 | * @return void 199 | */ 200 | public function moveFiles($source, $target, $files) 201 | { 202 | $this->mkDir($target); 203 | foreach ($files as $file => $value) { 204 | if (file_exists($source.$value)) { 205 | rename($source.$value, $target.$value); 206 | } 207 | } 208 | } 209 | 210 | /** 211 | * Delete files. 212 | * 213 | * @param (array) $file Name of file with path. 214 | * 215 | * @return void 216 | */ 217 | public function deleteFiles($files) 218 | { 219 | foreach ($files as $file => $value) { 220 | if (file_exists($value)) { 221 | unlink($value); 222 | } 223 | } 224 | } 225 | 226 | /** 227 | * Copy dirs. 228 | * 229 | * @param (string) $source Directory with path. 230 | * @param (string) $target Target directory 231 | * @param (array) $files Dirs to be copy 232 | * 233 | * @return void 234 | */ 235 | public function copyDirs($source, $target, $dirs) 236 | { 237 | $this->mkDir($target); 238 | $serverOs = (new \Zest\Common\OperatingSystem())->get(); 239 | $command = ($serverOs === 'Windows') ? 'xcopy ' : 'cp -r '; 240 | foreach ($dirs as $dir => $value) { 241 | if (is_dir($source.$value)) { 242 | shell_exec($command.$source.$value.' '.$target.$value); 243 | } 244 | } 245 | } 246 | 247 | /** 248 | * Move dirs. 249 | * 250 | * @param (string) $source Directory with path. 251 | * @param (string) $target Target directory 252 | * @param (array) $dir Dir to be move 253 | * 254 | * @return void 255 | */ 256 | public function moveDirs($source, $target, $dirs) 257 | { 258 | $this->mkDir($target); 259 | $command = ($serverOs === 'Windows') ? 'move ' : 'mv '; 260 | foreach ($dirs as $dir => $value) { 261 | if (is_dir($source.$value)) { 262 | shell_exec($command.$source.$value.' '.$target.$value); 263 | } 264 | } 265 | } 266 | 267 | /** 268 | * Delete dirs. 269 | * 270 | * @param (string) $dir Directory with path. 271 | * 272 | * @return void 273 | */ 274 | public function deleteDirs($dir) 275 | { 276 | foreach ($files as $file => $value) { 277 | if (is_dir($value)) { 278 | rmdir($value); 279 | } 280 | } 281 | } 282 | 283 | /** 284 | * Upload file. 285 | * 286 | * @param (string) $file File to be uploaded. 287 | * @param (string) $target Target where file should be upload 288 | * @param (string) $imgType Supported => image,media,docs,zip 289 | * @param (int) $maxSize File size to be allowed 290 | * 291 | * @return void 292 | */ 293 | public function fileUpload($file, $target, $imgType, $maxSize = 7992000000) 294 | { 295 | $exactName = basename($file['name']); 296 | $fileTmp = $file['tmp_name']; 297 | $fileSize = $file['size']; 298 | $error = $file['error']; 299 | $type = $file['type']; 300 | $ext = pathinfo($file['name'], PATHINFO_EXTENSION); 301 | $newName = $this->rendomFileName(30); 302 | $fileNewName = $newName.'.'.$ext; 303 | $allowerd_ext = $this->types[$imgType]; 304 | if (in_array($type, $this->mineTypes) === false) { 305 | return [ 306 | 'status' => 'error', 307 | 'code' => 'mineType', 308 | ]; 309 | } 310 | if (in_array($ext, $allowerd_ext) === true) { 311 | if ($error === 0) { 312 | if ($fileSize <= $maxSize) { 313 | $this->mkdir($target); 314 | $fileRoot = $target.$fileNewName; 315 | if (move_uploaded_file($fileTmp, $fileRoot)) { 316 | return [ 317 | 'status' => 'success', 318 | 'code' => $fileNewName, 319 | ]; 320 | } else { 321 | return [ 322 | 'status' => 'error', 323 | 'code' => 'somethingwrong', 324 | ]; 325 | } 326 | } else { 327 | return [ 328 | 'status' => 'error', 329 | 'code' => 'exceedlimit', 330 | ]; 331 | } 332 | } else { 333 | return [ 334 | 'status' => 'error', 335 | 'code' => $error, 336 | ]; 337 | } 338 | } else { 339 | return [ 340 | 'status' => 'error', 341 | 'code' => 'extension', 342 | ]; 343 | } 344 | } 345 | 346 | /** 347 | * Upload files. 348 | * 349 | * @param $files (array) files to be uploaded. 350 | * $target target where file should be upload 351 | * $imgType supported => image,media,docs,zip 352 | * $maxSize file size to be allowed 353 | * 354 | * @return void 355 | */ 356 | public function filesUpload($files, $target, $imgType, $count, $maxSize = 7992000000) 357 | { 358 | $status = []; 359 | for ($i = 0; $i < $count; $i++) { 360 | $exactName = basename($files['name'][$i]); 361 | $fileTmp = $files['tmp_name'][$i]; 362 | $fileSize = $files['size'][$i]; 363 | $error = $files['error'][$i]; 364 | $type = $files['type'][$i]; 365 | $ext = pathinfo($files['name'][$i], PATHINFO_EXTENSION); 366 | $newName = $this->rendomFileName(30); 367 | $fileNewName = $newName.'.'.$ext; 368 | $allowerd_ext = $this->types[$imgType]; 369 | if (in_array($type, $this->mineTypes) === false) { 370 | $status[$i] = [ 371 | 'status' => 'error', 372 | 'code' => 'mineType', 373 | ]; 374 | } 375 | if (in_array($ext, $allowerd_ext) === true) { 376 | if ($error === 0) { 377 | if ($fileSize <= $maxSize) { 378 | $this->mkdir($target); 379 | $fileRoot = $target.$fileNewName; 380 | if (move_uploaded_file($fileTmp, $fileRoot)) { 381 | $status[$i] = [ 382 | 'status' => 'success', 383 | 'code' => $fileNewName, 384 | ]; 385 | } else { 386 | $status[$i] = [ 387 | 'status' => 'error', 388 | 'code' => 'somethingwrong', 389 | ]; 390 | } 391 | } else { 392 | $status[$i] = [ 393 | 'status' => 'error', 394 | 'code' => 'exceedlimit', 395 | ]; 396 | } 397 | } else { 398 | $status[$i] = [ 399 | 'status' => 'error', 400 | 'code' => $error, 401 | ]; 402 | } 403 | } else { 404 | $status[$i] = [ 405 | 'status' => $error, 406 | 'code' => 'extension', 407 | ]; 408 | } 409 | } 410 | 411 | return $status; 412 | } 413 | 414 | /** 415 | * Open the file. 416 | * 417 | * @param (string) $name Name of file 418 | * @param (string) $mode Mode of file 419 | * 420 | * @return resource 421 | */ 422 | public function open($name, $mode) 423 | { 424 | if (!empty(trim($name))) { 425 | $this->resource = fopen($name, $this->modes[$mode]); 426 | 427 | return $this; 428 | } 429 | } 430 | 431 | /** 432 | * Read the file. 433 | * 434 | * @param (string) $file File that to be read 435 | * 436 | * @return file 437 | */ 438 | public function read($file) 439 | { 440 | return fread($this->resource, filesize($file)); 441 | } 442 | 443 | /** 444 | * Write on file. 445 | * 446 | * @param (string) $data Data that you want write on file 447 | * 448 | * @return bool 449 | */ 450 | public function write($data) 451 | { 452 | return (!empty($data)) ? fwrite($this->resource, $data) : false; 453 | } 454 | 455 | /** 456 | * Delete the file. 457 | * 458 | * @param $file file to be deleted 459 | * 460 | * @return bool 461 | */ 462 | public function delete($file) 463 | { 464 | return (file_exists($file)) ? unlink($file) : false; 465 | } 466 | 467 | /** 468 | * generate salts for files. 469 | * 470 | * @param (string) $length Length of salts 471 | * 472 | * @return string 473 | */ 474 | public static function rendomFileName($length) 475 | { 476 | $chars = array_merge(range(0, 9), range('a', 'z'), range('A', 'Z')); 477 | $stringlength = count($chars); //Used Count because its array now 478 | $randomString = ''; 479 | for ($i = 0; $i < $length; $i++) { 480 | $randomString .= $chars[rand(0, $stringlength - 1)]; 481 | } 482 | 483 | return $randomString; 484 | } 485 | } 486 | --------------------------------------------------------------------------------