为什么我通过file_get_contents下载文件会损坏文件


Why I am getting corrupted files by downloading it via file_get_contents?

    $source_path = 'https://bip.wzp.pl/attachments/' . $filename;  

    $opts = array(
      'http'=>array(
        'method'=>"GET",
        'header'=>"Content-type: application/pdf",
      )
    );
   $context = stream_context_create($opts);    
    $data = file_get_contents($source_path, false, $context);
    if(($handle = fopen($dest_path, "w"))) {
        fwrite($handle, $data);
        fclose($handle);
        return $filename_converted;
    }

例如$source_path

https://bip.wzp.pl/attachments/28172_Statut 沃耶沃兹特瓦 Zachodniopomorskiego.pdf

当我尝试浏览器时,pdf文档显示正确。

但是通过PHP下载的文件,它总是被损坏。

编辑

我检查了$data内容。它有首页html :/奇怪的是将$source_path放入浏览器返回pdf

似乎您尝试下载 PDF,请尝试使用以下方法将其另存为二进制文件:

fopen($dest_path, "wb")