dev/php/ftrunc.php

48 lines
1.1 KiB
PHP

#!/usr/bin/php
<?php
# ftrunc.php - a script to truncate files
# Copyright (C) 2008 Roman Mamedov <http://rm.pp.ru/>
#
# This program is free software: you can redistribute it and/or modify it under the terms of
# the GNU General Public License version 3 as published by the Free Software Foundation.
# See http://www.gnu.org/licenses/gpl-3.0.html for more details.
$file = $argv[1];
$size = $argv[2];
$unit = $argv[3];
if((trim($file)=="") || (!is_numeric($size)))
die ("Usage:\n ftrunc.php <file name> <new size> <size unit(K/M/G/T)>\n");
switch ($unit) {
case "K":
$size_b = $size*1024; break;
case "M":
$size_b = $size*1024*1024; break;
case "G":
$size_b = $size*1024*1024*1024; break;
case "T":
$size_b = $size*1024*1024*1024*1024; break;
default:
$unit = "";
$size_b = $size;
}
echo "File: $file\n";
if($unit!="") {
echo "New size: $size $unit ($size_b bytes)\n";
} else {
echo "New size: $size_b bytes\n";
}
$fh = fopen($file, "a");
if($fh===false) die("Unable to open file for writing.");
echo "Truncating... ";
if(ftruncate($fh, $size_b)) echo "Done.\n"; else echo "FAILED.\n";
fclose($fh);
?>