...

View Full Version : Getting file sizes



graham23s
08-28-2007, 08:21 AM
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:


<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

pemcconnell
08-28-2007, 12:52 PM
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.


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:



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

graham23s
08-28-2007, 01:38 PM
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

Mwnciau
08-28-2007, 01:57 PM
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.

graham23s
08-28-2007, 06:23 PM
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

Mwnciau
08-28-2007, 07:13 PM
$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

CFMaBiSmAd
08-28-2007, 07:23 PM
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) -

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;

graham23s
08-28-2007, 07:23 PM
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:


2048


is there a way to convert to a more readable form say MB and GB.

graham23s
08-28-2007, 07:24 PM
sorry dupe post

Mwnciau
08-28-2007, 07:28 PM
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.


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


P.S to delete your post click edit then delete.

Inigoesdr
08-28-2007, 07:31 PM
http://www.php.net/manual/en/function.filesize.php#62656

graham23s
08-28-2007, 07:48 PM
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

$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

Mwnciau
08-28-2007, 07:55 PM
Try it without $xml = simplexml_load_string($filecontent);.

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

graham23s
08-28-2007, 08:02 PM
Brilliant thanks guys just 1 last thing

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

Mwnciau
08-28-2007, 08:23 PM
echo $kbytes; // kilobytes
echo $mbytes; // megabytes
echo $gbytes; // gigabytes

graham23s
08-28-2007, 08:47 PM
Hi Mate,

when i echo out the gigabyte one for example i get:


1563698176
0.000954389572144

the top figure is the $sum and the second the $gbytes it's a really long integer is there a way i can do : 4.26gb <-- for example

thanks mate

Graham

Mwnciau
08-28-2007, 08:51 PM
http://www.php.net/manual/en/function.filesize.php#62656


<?php

function formatbytes($val, $digits = 3, $mode = "SI", $bB = "B"){ //$mode == "SI"|"IEC", $bB == "b"|"B"
$si = array("", "k", "M", "G", "T", "P", "E", "Z", "Y");
$iec = array("", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi");
switch(strtoupper($mode)) {
case "SI" : $factor = 1000; $symbols = $si; break;
case "IEC" : $factor = 1024; $symbols = $iec; break;
default : $factor = 1000; $symbols = $si; break;
}
switch($bB) {
case "b" : $val *= 8; break;
default : $bB = "B"; break;
}
for($i=0;$i<count($symbols)-1 && $val>=$factor;$i++)
$val /= $factor;
$p = strpos($val, ".");
if($p !== false && $p > $digits) $val = round($val);
elseif($p !== false) $val = round($val, $digits-$p);
return round($val, $digits) . " " . $symbols[$i] . $bB;
}

echo formatbytes($sum);

?>

graham23s
08-28-2007, 09:16 PM
i can't thank you guys enough i would never have got there by myself:)

thanks guys

Graham



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum