Remote file size without downloading file

前端 未结 14 1602
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 03:01

Is there a way to get the size of a remote file http://my_url/my_file.txt without downloading the file?

14条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 03:26

    Try this code

    function retrieve_remote_file_size($url){
         $ch = curl_init($url);
    
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
         curl_setopt($ch, CURLOPT_HEADER, TRUE);
         curl_setopt($ch, CURLOPT_NOBODY, TRUE);
    
         $data = curl_exec($ch);
         $size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
    
         curl_close($ch);
         return $size;
    }
    

提交回复
热议问题