Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过php curl下载文件时显示文件大小_Php_Curl_Download - Fatal编程技术网

通过php curl下载文件时显示文件大小

通过php curl下载文件时显示文件大小,php,curl,download,Php,Curl,Download,我想通过php中的curl强制将文件从服务器下载到本地系统 一切正常,但在开始下载时没有显示文件大小。 这是密码 $file_url ='http://example.com/dl/dlfile.zip'; header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: atta

我想通过php中的curl强制将文件从服务器下载到本地系统 一切正常,但在开始下载时没有显示文件大小。 这是密码

    $file_url ='http://example.com/dl/dlfile.zip';

    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file_url)); 
    // header('Content-Transfer-Encoding: chunked'); 
    header('Content-Transfer-Encoding: binary'); 
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    $stream = fopen('php://output', 'w');
    $ch = curl_init($file_url);

    curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file_url));

    curl_setopt($ch, CURLOPT_READFUNCTION, function($ch, $fd, $length) use ($stream) {
        return fwrite($stream, fread($fd, $length));
    });
    curl_exec($ch);
    curl_close($ch);
    exit();
当我使用这个片段时 filesize()在下载的文件中抛出错误,从而损坏文件。
我知道filesize函数使用的是绝对路径而不是url路径,但有没有办法解决这个问题?

内容处置
标题之前添加以下标题,以将文件大小发送到浏览器:

header("Content-Length: ".filesize($file_url));

有什么解决方案吗?我肯定有,但是很难测试,因为它有点上下文依赖性。