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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Regular Coder
    Join Date
    Jul 2007
    Location
    Scotland
    Posts
    134
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Getting file sizes

    Hi Guys,

    what im trying to do here is calculate file sizes in an .xml file for example this is the bit i need to calculate:

    PHP Code:
        <segments>
          <
    segment number="1" bytes="259072">DPydnTk-mLYWEU_bnZ2dnUVZ_s6mnZ2d@giganews.com</segment>
          <
    segment number="2" bytes="259072">DPydnTg-mLYWEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="3" bytes="259072">DPydnTs-mLYREU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="4" bytes="259072">DPydnTo-mLYREU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="5" bytes="259072">DPydnTU-mLYQEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="6" bytes="259072">DPydnTQ-mLYQEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="7" bytes="259072">DPydnTc-mLYTEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="8" bytes="259072">DPydnTY-mLYTEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="9" bytes="259072">DPydnTE-mLYSEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="10" bytes="259072">DPydnTA-mLYNEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="11" bytes="259072">DPydnTM-mLYNEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="12" bytes="259072">DPydnTI-mLYNEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="13" bytes="259072">DPydnS0-mLYMEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="14" bytes="259072">DPydnSw-mLYMEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="15" bytes="259072">DPydnS8-mLYPEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="16" bytes="259072">DPydnS4-mLYPEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="17" bytes="259072">DPydnSk-mLYPEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="18" bytes="259072">DPydnSg-mLYOEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="19" bytes="259072">DPydnSs-mLYOEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="20" bytes="259072">DPydnSo-mLYJEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="21" bytes="259072">DPydnSU-mLYJEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="22" bytes="259072">DPydnSQ-mLYIEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="23" bytes="259072">DPydnSc-mLYIEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="24" bytes="259072">DPydnSY-mLYIEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="25" bytes="259072">DPydnSE-mLYLEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="26" bytes="259072">DPydnSA-mLYLEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="27" bytes="259072">DPydnSM-mLYKEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="28" bytes="259072">DPydnSI-mLYKEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="29" bytes="259072">DPydnV0-mLYKEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="30" bytes="259072">DPydnVw-mLYFEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="31" bytes="259072">DPydnV8-mLYFEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="32" bytes="259072">DPydnV4-mLYEEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="33" bytes="259072">DPydnVk-mLYEEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="34" bytes="259072">DPydnVg-mLYHEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="35" bytes="259072">DPydnVs-mLYHEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="36" bytes="259072">DPydnVo-mLYHEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="37" bytes="259072">DPydnVU-mLYGEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="38" bytes="259072">DPydnVQ-mLYGEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="39" bytes="259072">DPydnVc-mLYBEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="40" bytes="259072">DPydnVY-mLYBEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="41" bytes="259072">DPydnVE-mLYAEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="42" bytes="259072">DPydnVA-mLYAEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="43" bytes="259072">DPydnVM-mLYDEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="44" bytes="259072">DPydnVI-mLYDEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="45" bytes="259072">DPydnU0-mLYDEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="46" bytes="259072">DPydnUw-mLYCEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="47" bytes="259072">DPydnU8-mLYCEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="48" bytes="259072">DPydnU4-mLY9EU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="49" bytes="259072">DPydnUk-mLY9EU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="50" bytes="259072">DPydnUg-mLY8EU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="51" bytes="259072">DPydnUs-mLY8EU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="52" bytes="259072">DPydnUo-mLY8EU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="53" bytes="259072">DPydnUU-mLY_EU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="54" bytes="259072">DPydnUQ-mLY_EU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="55" bytes="259072">DPydnUc-mLY-EU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="56" bytes="259072">DPydnUY-mLY-EU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="57" bytes="259072">DPydnUE-mLY5EU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="58" bytes="259072">DPydnUA-mLY5EU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="59" bytes="259072">DPydnUM-mLY4EU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="60" bytes="259072">DPydnUI-mLY4EU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
          <
    segment number="61" bytes="25600">DPydnX0-mLY7EU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment>
        </
    segments
    using a preg_match_all i can count the number of 'bytes=' but is there a way i can extract the value of bytes, then i can times it by the number of preg_match_all results or is there a better way?

    thanks guys

    Graham

  • #2
    New Coder
    Join Date
    Feb 2007
    Location
    Near Belfast
    Posts
    38
    Thanks
    0
    Thanked 1 Time in 1 Post
    Best way to do it is to avoid the entire xml integration, and just use the $_FILES array. This also will give you a more accurate value as file sizes may vary, and youre not just taking an average and multiplying it.

    *Hope I understood the question

    eg.
    Code:
    for($x=0; $x<$totalFiles; $x++){
          // place switch statement or increment value, to pull all file names here
          // e.g. $filename = 'FOLDERNAME/image0'.$x.'.jpg';
          $singlefilesize = $_FILES[$filename]["size"] / 1024);
          $allfilesizes = $allfilesizes + $singlefilesize;
    }
    echo 'All files in folder come to '.$allfilesizes.' kb';
    // to get total bytes, take /1024 off
    // google the $_FILES function for a detailed instruction
    Also, if your data is stored / referenced on a database, just reference it in the following way:

    Code:
    while($row = mysql_fetch_array($datasource)){
          $filename = $row['filename'];
          $singlefilesize = $_FILES[$filename]["size"] / 1024);
          $allfilesizes = $allfilesizes + $singlefilesize;
    }
    This will also alow for very easy file upload integration, if it is ever needed.

    Hope this helps
    Last edited by pemcconnell; 08-28-2007 at 01:00 PM.
    Knowledge is only half the value of passion. But passion doesn't pay the bills.

  • #3
    Regular Coder
    Join Date
    Jul 2007
    Location
    Scotland
    Posts
    134
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Hi Mate,

    it's actually the data inside the xml im trying to get the value of (i should have put a better heading lol)

    Graham

  • #4
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    PHP Code:
    preg_match_all('#bytes="\d{1,6}"#i'$xml$out);
    $bytes = (sizeof($out[0])-1) * 259072 $out[0][sizeof($out[0])]; 
    That will count how many times bytes="1-6 numbers" and work out the bytes form there.

  • #5
    Regular Coder
    Join Date
    Jul 2007
    Location
    Scotland
    Posts
    134
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Hi,

    the only things is , all files will be different when uploaded "259072" is just the number in this example could i replace that with a variable at all?

    cheers

    Graham

  • #6
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    PHP Code:
    $bytes 259072;
    preg_match_all('#bytes="\d{1,6}"#i'$xml$out);
    $bytes = (sizeof($out[0])-1) * $bytes $out[0][sizeof($out[0])]; 
    Just change the bytes variable

  • #7
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,044
    Thanks
    2
    Thanked 316 Times in 308 Posts
    Generic code using Mwnciau's preg_match_all (Note: The file size could be more than 6 digits, so I adjusted the maximum to 15) that will add up any size files (tested on the .xml in the first post) -
    PHP Code:
    preg_match_all('#bytes="\d{1,15}"#i'$xml$out); // use previously posted preg_match_all

    $search = array('bytes=','"'); // array of things to search for

    $sizes str_replace($search""$out[0]); // strip off any things in the $search array

    $sum array_sum($sizes); // get the sum
    echo $sum
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #8
    Regular Coder
    Join Date
    Jul 2007
    Location
    Scotland
    Posts
    134
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Hi Mate,

    thats perfect results coming back great for example:

    1 files i test upload is around 700mb it comes back this for the size:

    PHP Code:
    2048 
    is there a way to convert to a more readable form say MB and GB.

  • #9
    Regular Coder
    Join Date
    Jul 2007
    Location
    Scotland
    Posts
    134
    Thanks
    12
    Thanked 0 Times in 0 Posts
    sorry dupe post

  • #10
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    PHP Code:
    preg_match_all('#(?<=bytes=")\d{1,15}(!?")#i'$xml$out); // doesn't match the bytes=" and " but checks for them

    array_sum($out[0]);

    echo 
    $sum
    I think that is more efficient, was going to add negative lookahead and negative lookbehind in my original post but forgot to.


    is there a way to convert to a more readable form say MB and GB.
    PHP Code:
    $bytes 1024768;
    $kbytes $bytes 1024;
    $mbytes $kbytes 1024;
    $gbytes $mbytes 1024
    P.S to delete your post click edit then delete.
    Last edited by Mwnciau; 08-28-2007 at 07:31 PM.

  • #11
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts

  • #12
    Regular Coder
    Join Date
    Jul 2007
    Location
    Scotland
    Posts
    134
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Hi guys,

    thanks a ton for all your help im 95% there.

    i was trying to test the code above in a seperate file i tried:

    PHP Code:
    <?php

    $filecontent 
    file_get_contents("filename.xml");

    $xml simplexml_load_string($filecontent);

    preg_match_all('#bytes="\d{1,15}"#i'$xml$out); // use previously posted preg_match_all 

    $search = array('bytes=','"'); // array of things to search for 

    $sizes str_replace($search""$out[0]); // strip off any things in the $search array 

    $sum array_sum($sizes); // get the sum 

    echo $sum

    ?>
    but this returns 0, have i typed this correctly?

    cheers guys

    Graham

  • #13
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    Try it without $xml = simplexml_load_string($filecontent);.

    $xml = simplexml_load_string($filecontent); returns an array, not a string.

  • #14
    Regular Coder
    Join Date
    Jul 2007
    Location
    Scotland
    Posts
    134
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Brilliant thanks guys just 1 last thing

    code:

    PHP Code:
    <?php

    $filecontent 
    file_get_contents("filename.xml");

    //$xml = simplexml_load_string($filecontent);

    preg_match_all('#bytes="\d{1,15}"#i'$filecontent$out); // use previously posted preg_match_all 

    $search = array('bytes=','"'); // array of things to search for 

    $sizes str_replace($search""$out[0]); // strip off any things in the $search array 

    $sum array_sum($sizes); // get the sum 

    $bytes 1024768;
    $kbytes $bytes 1024;
    $mbytes $kbytes 1024;
    $gbytes $mbytes 1024;  

    echo 
    $sum

    ?>
    what is the right context to get the file size? $sum is the total which echoes: 1563698176

    how would i get the size in GB for example?

    thanks again guys

    Graham

  • #15
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    PHP Code:
    echo $kbytes// kilobytes
    echo $mbytes// megabytes
    echo $gbytes// gigabytes 


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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