PHP: get remote file size with strlen? (html)

前端 未结 3 1887
梦如初夏
梦如初夏 2021-01-15 03:58

I was looking at PHP docs for fsockopen and whatnot and they say you can\'t use filesize() on a remote file without doing some crazy things with ftell or something (not sure

3条回答
  •  时光说笑
    2021-01-15 04:04

    This answer requires PHP5 and cUrl. It first checks the headers. If Content-Length isn't specified, it uses cUrl to download it and check the size (the file is not saved anywhere though--just temporarily in memory).

     $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HTTPHEADER => array('User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3'),
            ));
        curl_exec($c);
        return curl_getinfo($c, CURLINFO_SIZE_DOWNLOAD);
    }
    ?>
    

提交回复
热议问题