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
    Registered User
    Join Date
    Sep 2005
    Posts
    315
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation formating filesize in bytes => mbytes

    Hi, me again with another problem i have this code (working fine AT LAST thanx to this forum ) BUT i want to format the bytes into megabytes, can somebody give me the exact codes (elseway i'll screw it up again ).
    PHP Code:
    <?

    function getUsedMemory($login

      
    // HAAL ALLE LINKS VAN EEN BEPAALDE PERSOON EN BEKIJK GROOTTE
      
    $sql 'SELECT bestand FROM uploads WHERE lid ="'.$login.'"';  
      
    $resultSet mysql_query($sql) or die(mysql_error()); 
      
    $memory 0
      if(
    mysql_num_rows($resultSet) != 0
      { 
        while(
    $row mysql_fetch_row($resultSet)) 
        { 
          
    // row[0] is de url naar een bestand dat geupload is door gebruiker 
          
    $memory += filesize($row[0]); 
        } 
      } 
      return 
    $memory



    // haal totaal van alle geuploade afbeeldingen 
    $totalSize getUsedMemory("$login"); 
    // maximum is 3 MB 
    $maxSize 3<<20
    // als het groter is dan maximum
    if($totalSize >= $maxSize

      echo 
    "Je hebt het maximum van 3MB overschreden. Je moet foto's verwijderen voor je er nieuwe kan uploaden!"

    else 

      echo 
    'Je hebt ',$maxSize-$totalSize,' byte(s) over die je nog kan gebruiken voor het uploaden van fotos.'
    }  

    ?>
    SO, $maxsize-$totalsize should be XXXX megabyte (or x,xx megabyte) please help thanx !

  • #2
    Regular Coder devinemke's Avatar
    Join Date
    Dec 2004
    Location
    NYC
    Posts
    443
    Thanks
    0
    Thanked 12 Times in 11 Posts
    a simple fucntion for converting bytes to B,KB,MB,GB:
    PHP Code:
    function format_size($bytes)
    {
        
    $size_array = array(=> 'B'1024 => 'KB'1048576 => 'MB'1073741824 => 'GB');
        foreach (
    $size_array as $key => $value)
        {
            if (
    $bytes >= $key)
            {
                
    $formatted number_format(round($bytes $key2) , 2) . ' ' $value;
            }
        }
        return 
    $formatted;


  • #3
    Registered User
    Join Date
    Sep 2005
    Posts
    315
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    <?

    function getUsedMemory($login

      
    // HAAL ALLE LINKS VAN EEN BEPAALDE PERSOON EN BEKIJK GROOTTE
      
    $sql 'SELECT bestand FROM uploads WHERE lid ="'.$login.'"';  
      
    $resultSet mysql_query($sql) or die(mysql_error()); 
      
    $memory 0
      if(
    mysql_num_rows($resultSet) != 0
      { 
        while(
    $row mysql_fetch_row($resultSet)) 
        { 
          
    // row[0] is de url naar een bestand dat geupload is door gebruiker 
          
    $memory += filesize($row[0]); 
        } 
      } 
      return 
    $memory

    function 
    format_size($totalsize

        
    $size_array = array(=> 'B'1024 => 'KB'1048576 => 'MB'1073741824 => 'GB'); 
        foreach (
    $size_array as $key => $value
        { 
            if (
    $bytes >= $key
            { 
                
    $formatted number_format(round($bytes $key2) , 2) . ' ' $value
            } 
        } 
        return 
    $formatted
    }  



    // haal totaal van alle geuploade afbeeldingen 
    $totalSize getUsedMemory("$login"); 
    $totalSize format_size("$totalSize"); 
    // maximum is 3 MB 
    $maxSize 3<<20
    // als het groter is dan maximum
    if($totalSize >= $maxSize

      echo 
    "Je hebt het maximum van 3MB overschreden. Je moet foto's verwijderen voor je er nieuwe kan uploaden!"

    else 

      echo 
    'Je hebt ',$maxSize-$totalSize,' byte(s) over die je nog kan gebruiken voor het uploaden van fotos.'
    }  

    ?>
    Like this? (sorry i'm a total noob)

  • #4
    Registered User
    Join Date
    Sep 2005
    Posts
    315
    Thanks
    0
    Thanked 0 Times in 0 Posts
    can anyone insert the function file_size into the original script? Cause i'm not good at working with functions. Please? THNX !

  • #5
    Registered User
    Join Date
    Sep 2005
    Posts
    315
    Thanks
    0
    Thanked 0 Times in 0 Posts
    please

  • #6
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by arne
    please
    yes, bro, I beleive you got it. Congratulations.

  • #7
    Registered User
    Join Date
    Sep 2005
    Posts
    315
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i don't think so, it's still XXXX bytes

  • #8
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post
    well, its probobly because the number is changing, but your not changing the text that goes with the number.


  •  

    Posting Permissions

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