Search code examples
phppdfcurldropbox

Why can't get the length of the pdf file with `CURLINFO_CONTENT_LENGTH_DOWNLOAD` in php_curl?


I have to get filesize of a remote file by using curl in PHP the file is hosted on Dropbox which will be downloaded by direct link below is my code what I have written

$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);

The output is -1 with the code, How to get the length of the iso file properly with php_curl?


Solution

  • You have to set the curl options before you exec:

    <?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);