Skip to content

Instantly share code, notes, and snippets.

@LiamKarlMitchell
Created August 29, 2018 21:18
Show Gist options
  • Save LiamKarlMitchell/145a06b2ad2da0e24d280a14d234ac69 to your computer and use it in GitHub Desktop.
Save LiamKarlMitchell/145a06b2ad2da0e24d280a14d234ac69 to your computer and use it in GitHub Desktop.
A php script to read a input txt file reading the contents of lines as file paths and totaling the size of those files.
<?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 ++;
// Note: Won't work on files > 2GB? see php manual.
$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