...

View Full Version : Convert bytes to megs for file size



sonny
07-27-2009, 10:27 PM
I have a backup script that shows the file size in bytes
is there a way to display Megs instead these two lines
are the only code I see related to that in the script.

if ($mat[7] <= 0)

File Size Is $mat[7] Byte

Thanks

KevinADC
07-27-2009, 10:37 PM
to convert bytes into megabytes divide by 1024 or use a module that does the conversions for you, I'm sure there are some written to do that. The code you posted is so out of context there is no way to tell why or what it is doing besides checking that the value of $mat[7] is less than or equal to zero. Which I guess indicates it is less than one megabyte so it needs to print the byte size instead, although I think that would be written like this:

if ($mat[7] <= 1)

sonny
07-27-2009, 11:00 PM
to convert bytes into megabytes divide by 1024 or use a module that does the conversions for you, I'm sure there are some written to do that. The code you posted is so out of context there is no way to tell why or what it is doing besides checking that the value of $mat[7] is less than or equal to zero. Which I guess indicates it is less than one megabyte so it needs to print the byte size instead, although I think that would be written like this:

if ($mat[7] <= 1)



$hitch = ();
($day, $month, $year) = (localtime)[3,4,5];
$year = $year + 1900; $month++;
if ($date_yes) { $hitch = "$year-$month-$day"; }
print "ARCHIVING FILES\n";
@the = `tar -czf $tarpath/Backup_$hitch.tar.gz $basepath`;
@mat = stat("$tarpath/Backup_$hitch.tar.gz");
if ($mat[7] <= 0) { print "Backup didnt happen. The error returned by the system (if any) is @the\n"; &send("Backup FAILED. Tar did not backup any files. Error message returned from server (if any) is @the", "Backup FAILED"); exit; }
print "Files Archived to Backup_$hitch.tar.gz<br><br>File Size Is $mat[7] Bytes<br><br>\n";
if ($just_back_up) {



That's basically the code involved, those two lines is all I see related to
bytes, I am on a shared host so whatever m7 is seems to be whats doing it

KevinADC
07-27-2009, 11:23 PM
OK, see how this works for you:



$hitch = ();
($day, $month, $year) = (localtime)[3,4,5];
$year = $year + 1900; $month++;
if ($date_yes) { $hitch = "$year-$month-$day"; }
print "ARCHIVING FILES\n";
@the = `tar -czf $tarpath/Backup_$hitch.tar.gz $basepath`;
@mat = stat("$tarpath/Backup_$hitch.tar.gz");
if ($mat[7] <= 0) {
print "Backup didnt happen. The error returned by the system (if any) is @the\n";
&send("Backup FAILED. Tar did not backup any files. Error message returned from server (if any) is @the", "Backup FAILED");
exit;
}
if ($mat[7] > 1023) {
$mat[7] = sprintf("%.2f MB",$mat[7]/1024);#<-- Convert to MBs to two decimal places
}
else {
$mat[7] .= ' Bytes';
}
print "Files Archived to Backup_$hitch.tar.gz<br><br>File Size Is $mat[7]<br><br>\n";
if ($just_back_up) {

sonny
07-27-2009, 11:47 PM
I get this
Backup size 19084.43 MB bytes


The actual size of the file is 18.64 MiB

KevinADC
07-27-2009, 11:53 PM
sorry, don't know what I was thinking, these lines:


if ($mat[7] > 1023) {
$mat[7] = sprintf("%.2f MB",$mat[7]/1024);#<-- Convert to MBs to two decimal places
}

should be:


if ($mat[7] > 1048575) {
$mat[7] = sprintf("%.2f MB",$mat[7]/1024/1024);#<-- Convert to MBs to two decimal places
}


1024 bytes is one kilobyte, not one megabyte, 1048576 bytes is one megabyte

sonny
07-28-2009, 12:07 AM
sorry, don't know what I was thinking, these lines:


if ($mat[7] > 1023) {
$mat[7] = sprintf("%.2f MB",$mat[7]/1024);#<-- Convert to MBs to two decimal places
}

should be:


if ($mat[7] > 1048575) {
$mat[7] = sprintf("%.2f MB",$mat[7]/1024/1024);#<-- Convert to MBs to two decimal places
}


1024 bytes is one kilobyte, not one megabyte, 1048576 bytes is one megabyte

PHP reads it as 18.6 meg
XP says its 18.64 meg

and the backup script shows this
Backup size 19084.45 MB bytes

It appears to be a Meg off for some reason

sonny
07-28-2009, 12:19 AM
Works Perfect, sorry about that I didn't replace both lines

Thank you!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum