为什么用php_curl中的`CURLINFO_CONTENT_LENGTH_DOWNLOAD`无法获得pdf文件的长度?

时间:2019-05-09 17:28:16

标签: php pdf curl dropbox

我必须通过在PHP中使用curl来获取远程文件的文件大小 该文件托管在Dropbox上,将通过直接链接下载 以下是我写的代码

$szUrl='https://www.dl.dropboxusercontent.com/s/8ol4o753smure2o/Holy_Quran_in_Turkish.pdf';
    $curl = curl_init();
    curl_exec($curl);


    curl_setopt($curl, CURLOPT_URL, $szUrl);
    curl_setopt($curl, CURLOPT_HEADER, 1);
    curl_setopt($curl, CURLOPT_NOBODY, 1);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);


   $size = curl_getinfo($curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
  echo $size;

curl_close($curl);

代码输出为-1,如何使用php_curl正确获取iso文件的长度?

1 个答案:

答案 0 :(得分:1)

您必须在执行前设置curl选项:

<?php

   $szUrl='https://www.dl.dropboxusercontent.com/s/8ol4o753smure2o/Holy_Quran_in_Turkish.pdf';
   $curl = curl_init();
   curl_setopt($curl, CURLOPT_URL, $szUrl);
   curl_setopt($curl, CURLOPT_HEADER, 1);
   curl_setopt($curl, CURLOPT_NOBODY, 1);
   curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
   curl_exec($curl);
   $size = curl_getinfo($curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
   echo $size;
   curl_close($curl);