Last active
August 24, 2018 23:15
-
-
Save sutlxwhx/14e01ec20a92ee6e327990f9424132af to your computer and use it in GitHub Desktop.
Sort files in the folder by their file size and retrieve it as an array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author https://gist.github.com/sutlxwhx | |
* | |
* @version 0.2 | |
* | |
* @license Apache License 2.0 | |
* | |
*/ | |
namespace sutlxwhx; | |
class Sizer | |
{ | |
/** | |
* @var string Set the property that will store the result | |
*/ | |
public $result; | |
/** | |
* Sizer constructor. | |
* @param $folder_name Absolute path to the folder which you want to process | |
*/ | |
public function __construct($folder_name) | |
{ | |
$this->working_folder = $folder_name . DIRECTORY_SEPARATOR; | |
if ($this->validate()) { | |
return $this->result = $this->process(); | |
} else { | |
return $this->result = "Directory that you entered in not readable by the script. Check the reading permissions or if it even exists."; | |
} | |
} | |
/** | |
* Check the availability of the input directory using ternary operators | |
*/ | |
private function validate() | |
{ | |
return is_readable($this->working_folder) ? true : false; | |
} | |
/** | |
* Specify the evaluating process | |
*/ | |
private function process() | |
{ | |
$list_of_files = array_values(array_diff(scandir($this->working_folder), array('..', '.'))); | |
foreach ($list_of_files as $filename) { | |
$list_of_file_sizes["$filename"] = filesize($this->working_folder . $filename); | |
} | |
arsort($list_of_file_sizes, SORT_NATURAL); | |
$list_of_files = array_values(array_flip($list_of_file_sizes)); | |
return $list_of_files; | |
} | |
} | |
/** | |
* Initialise Sizer class and set the necessary parameter | |
* Dump the result element of an object | |
*/ | |
/* $sizer_worker = new Sizer(__DIR__ . DIRECTORY_SEPARATOR . 'test'); */ | |
/* var_dump($sizer_worker->result); */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment