Created
August 29, 2018 21:18
-
-
Save LiamKarlMitchell/3369bc93aa5e550b779be4ab1988155d to your computer and use it in GitHub Desktop.
A php script which read a input txt file containing files to total the size of.
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 | |
| // Read filenames from the input file, get their file sizes if possible and total it. | |
| // Show the total in a human readable file size. | |
| function human_filesize($bytes, $decimals = 2) { | |
| $size = array('B','kB','MB','GB','TB','PB','EB','ZB','YB'); | |
| $factor = floor((strlen($bytes) - 1) / 3); | |
| return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$size[$factor]; | |
| } | |
| $total = 0; | |
| $count = 0; | |
| $count_unable = 0; | |
| $count_readfilesize = 0; | |
| $handle = fopen("./filesToDelete.txt", "r"); | |
| if ($handle) { | |
| while (($line = fgets($handle)) !== false) { | |
| // Deal to any line endings? | |
| $line = rtrim($line); | |
| $count ++; | |
| $size = @filesize($line); | |
| if ($size !== false) { | |
| $total += $size; | |
| //echo "$line is ".human_filesize($size)."<br>"; | |
| $count_readfilesize ++; | |
| } else { | |
| $count_unable ++; | |
| } | |
| } | |
| fclose($handle); | |
| } else { | |
| die('Error opening input file.'); | |
| } | |
| echo "Total Files: $count<br>"; | |
| echo "Files Read: $count_readfilesize<br>"; | |
| echo "Unable to read count: $count_unable<br>"; | |
| echo "Total Size of files: ".human_filesize($total)."<br>"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment