Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts

    Convert bytes to megs for file size

    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

  • #2
    Senior Coder
    Join Date
    Mar 2006
    Posts
    1,274
    Thanks
    2
    Thanked 39 Times in 38 Posts
    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)

  • #3
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by KevinADC View Post
    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)
    Code:
    $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

  • #4
    Senior Coder
    Join Date
    Mar 2006
    Posts
    1,274
    Thanks
    2
    Thanked 39 Times in 38 Posts
    OK, see how this works for you:

    Code:
    $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) {

  • #5
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    I get this
    Backup size 19084.43 MB bytes


    The actual size of the file is 18.64 MiB

  • #6
    Senior Coder
    Join Date
    Mar 2006
    Posts
    1,274
    Thanks
    2
    Thanked 39 Times in 38 Posts
    sorry, don't know what I was thinking, these lines:

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

    Code:
    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
    Last edited by KevinADC; 07-27-2009 at 11:56 PM.

  • Users who have thanked KevinADC for this post:

    sonny (07-28-2009)

  • #7
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by KevinADC View Post
    sorry, don't know what I was thinking, these lines:

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

    Code:
    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

  • #8
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts

    Resolved

    Works Perfect, sorry about that I didn't replace both lines

    Thank you!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •