图片的cURL下载is';t图像


cURL Download of image isn't an image

我正在通过curl从filepicker.io下载一个图像。下面是下载代码:

if ($_GET['download']== 'true'){
          $downloadarray = array();
          while($row = mysql_fetch_array($res)){
            $url= $row['loc'];
            $path = 'tmp/';
            $path .= rand(100,999);
            $path .= $row['name'];
            $fp = fopen($path, 'w');
            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_FILE, $fp);     
            $data = curl_exec($ch);     
            curl_close($ch);
            fclose($fp);
            echo print_r($data);
            $downloadarray[] =  array($path, $row['name']);
          }

         $zipname = rand(0,9999) . 'download.zip';
          $zip = new ZipArchive;
          $zip->open($zipname, ZipArchive::CREATE);
          foreach ($downloadarray as $file) {
            $zip->addFile($file['0'], $file['1']);
          }
          $zip->close();
          header('Content-Type: application/zip');
          header('Content-disposition: attachment; filename=' . $zipname);
          header('Content-Length: ' . filesize($zipname));
          readfile($zipname);
          unlink($zipname);
        }

出于某种原因,下载的文件是一个图像,例如"palm tree.jpeg",但它实际上并没有作为图像存储。图像文件中没有添加任何标头信息。当我在mac上用"获取信息"打开图像时,预览会正确渲染,但文件类型列为"文档"。我做错什么了吗?

编辑:现在添加了完整的代码,包括压缩

删除:

fwrite($fp, $data);

当您使用CURLOPT_FILE时,cURL将结果写入文件,您不需要自己执行。$data包含true,您将其附加到文件中(实际上,您将1附加到文件,因为您在编写true时将其转换为字符串)。