Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.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_Filepicker.io - Fatal编程技术网

Php cURL下载的图像不是';这不是图像

Php cURL下载的图像不是';这不是图像,php,curl,filepicker.io,Php,Curl,Filepicker.io,我正在通过curl从filepicker.io下载一个图像。以下是下载代码: if ($_GET['download']== 'true'){ $downloadarray = array(); while($row = mysql_fetch_array($res)){ $url= $row['loc']; $path = 'tmp/'; $path .= rand(100,999)

我正在通过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上用Get Info打开图像时,预览会正确呈现,但文件类型列为Document。我做错什么了吗

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

删除:

fwrite($fp, $data);
当您使用
CURLOPT_FILE
时,cURL会将结果写入该文件,而无需自己执行
$data
包含
true
,并且您正在将其附加到文件中(实际上您正在将
1
附加到文件中,因为您在编写文件时正在将
true
转换为字符串)。

删除:

fwrite($fp, $data);

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

这样做,但是zip文件现在已损坏,向OQ添加了zip代码。这样做了,但是zip文件现在已损坏,向OQ添加了zip代码。