Return to Snippet

Revision: 60100
at October 20, 2012 11:43 by o0110o


Updated Code
function file_size($_path, $_unit, $_float = null, $_username = null, $_password = null) { 
  $_unit = trim(strtoupper($_unit)); //Making the unit compatible
  function unit_size($data, $_unit, $_float) { 
    if (!isset($_float)) {$_float = 3;} // float 3 decimal places by default
    $sizes = array("B"=>0, "KB"=>1, "MB"=>2, "GB"=>3, "TB"=>4, "PB"=>5, "EB"=>6, "ZB"=>7, "YB"=>8); //Associated with each unit is the exponent that will be used to do the conversion from bytes
    if (array_key_exists($_unit, $sizes) && $sizes[$_unit] != 0) { // If the unit is not bytes we get down to business
      $number = $sizes[$_unit];
      $total = $data / (pow(1024, $number));
      return round($total, $_float)." ".$_unit;
    }
    else {return $data." B";} // If you want bytes then we just skip to this part
  }
  $ch = curl_init($_path);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($ch, CURLOPT_HEADER, TRUE);
  if(isset($userame) && isset($password)) { $headers = array('Authorization: Basic '.base64_encode("$user:$pw")); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); }
  curl_setopt($ch, CURLOPT_NOBODY, TRUE);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
  curl_setopt($ch, CURLOPT_URL, $_path); //specify the ur
  $data = curl_exec($ch);
  $size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
  curl_close($ch);
  return unit_size($size, $_unit, $_float); 
}

Revision: 60099
at October 20, 2012 10:22 by o0110o


Updated Code
function file_size($_path, $_unit, $_float = null, $_username = null, $_password = null) { //Accepts path, unit, float, username & password; In the listed order.
  $unit = trim(strtoupper($unit)); //Making the unit compatible
  function unit_size(&$data, $unit, $float) { 
    if (!isset($float)) {$float = 3;} // float 3 decimal places by default
    $sizes = array("B"=>0, "KB"=>1, "MB"=>2, "GB"=>3, "TB"=>4, "PB"=>5, "EB"=>6, "ZB"=>7, "YB"=>8); //Associated with each unit is the exponent that will be used to do the conversion from bytes
    if (array_key_exists($unit, $sizes) && $sizes[$unit] != 0) { // If the unit is not bytes we get down to business
      $number = $sizes[$unit];
      $total = $data / (pow(1024, $number));
      return round($total, $float)." ".$unit;
    }
    else {return $data." B";} // If you want bytes then we just skip to this part
  }
  $ch = curl_init($_path);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($ch, CURLOPT_HEADER, TRUE);
  if(isset($userame) && isset($password)) { $headers = array('Authorization: Basic '.base64_encode("$user:$pw")); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); }
  curl_setopt($ch, CURLOPT_NOBODY, TRUE);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
  curl_setopt($ch, CURLOPT_URL, $_path); //specify the ur
  $data = curl_exec($ch);
  $size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
  curl_close($ch);
  return unit_size($size, $unit, $float); 
}

Revision: 60098
at October 20, 2012 10:06 by o0110o


Initial Code
function file_size($_path, $_unit, $_float = null, $_username = null, $_password = null) { //Accepts path, unit, float, username & password; In the listed order.
  $unit = trim(strtoupper($unit)); //Making the unit compatible
  function unit_size(&$data, $unit, $float) { 
    if (!isset($float)) {$float = 3;} // float 3 decimal places by default
    $sizes = array("B"=>0, "KB"=>1, "MB"=>2, "GB"=>3, "TB"=>4, "PB"=>5, "EB"=>6, "ZB"=>7, "YB"=>8); //Associated with each unit is the exponent that will be used to do the conversion from bytes
    if (array_key_exists($unit, $sizes) && $sizes[$unit] != 0) { // If the unit is not bytes we get down to business
      $number = $sizes[$unit];
      $total = $data / (pow(1024, $number));
      return round($total, $float)." ".$unit;
      exit();
    }
    else {return $data." B"; exit();} // If you want bytes then we just skip to this part
  }
  $ch = curl_init($_path);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($ch, CURLOPT_HEADER, TRUE);
  if(isset($userame) && isset($password)) { $headers = array('Authorization: Basic '.base64_encode("$user:$pw")); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); }
  curl_setopt($ch, CURLOPT_NOBODY, TRUE);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
  curl_setopt($ch, CURLOPT_URL, $_path); //specify the ur
  $data = curl_exec($ch);
  $size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
  curl_close($ch);
  return unit_size($size, $unit, $float); 
  exit();
}

Initial URL


Initial Description
A function to get a files size via curl. Accepts $_path, $_unit, $_float, $_username & $_password; In the listed order.

Initial Title
A function to get file sizes in multiple formats with curl.

Initial Tags
curl, php, function, files

Initial Language
PHP