使用curl获取多种格式文件大小的函数。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用curl获取多种格式文件大小的函数。相关的知识,希望对你有一定的参考价值。

A function to get a files size via curl. Accepts $_path, $_unit, $_float, $_username & $_password; In the listed order.
  1. function file_size($_path, $_unit, $_float = null, $_username = null, $_password = null) {
  2. $_unit = trim(strtoupper($_unit)); //Making the unit compatible
  3. function unit_size($data, $_unit, $_float) {
  4. if (!isset($_float)) {$_float = 3;} // float 3 decimal places by default
  5. $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
  6. if (array_key_exists($_unit, $sizes) && $sizes[$_unit] != 0) { // If the unit is not bytes we get down to business
  7. $number = $sizes[$_unit];
  8. $total = $data / (pow(1024, $number));
  9. return round($total, $_float)." ".$_unit;
  10. }
  11. else {return $data." B";} // If you want bytes then we just skip to this part
  12. }
  13. $ch = curl_init($_path);
  14. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  15. curl_setopt($ch, CURLOPT_HEADER, TRUE);
  16. if(isset($userame) && isset($password)) { $headers = array('Authorization: Basic '.base64_encode("$user:$pw")); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); }
  17. curl_setopt($ch, CURLOPT_NOBODY, TRUE);
  18. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  19. curl_setopt($ch, CURLOPT_URL, $_path); //specify the ur
  20. $data = curl_exec($ch);
  21. $size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
  22. curl_close($ch);
  23. return unit_size($size, $_unit, $_float);
  24. }

以上是关于使用curl获取多种格式文件大小的函数。的主要内容,如果未能解决你的问题,请参考以下文章

php使用curl下载指定大小的文件

php使用curl下载指定大小的文件

分享几个实用的代码片段(第二弹)

分享几个实用的代码片段(第二弹)

常用python日期日志获取内容循环的代码片段

将 Postman“获取新的授权令牌”转换为 curl 命令