I'm downloading an image via curl from filepicker.io. Here's the downloading code:
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);
}
For some reason the downloaded file is an image, for example 'palm-tree.jpeg' but it isn't actually being stored as an image. There is no header information being added in the image file. When I open the image with Get Info on my mac a preview correctly renders but the file type is listed as Document. Am I doing something wrong?
EDIT: Now added full code including zipping
Remove:
fwrite($fp, $data);
When you use CURLOPT_FILE
, cURL writes the results to the file, you don't need to do it yourself. $data
contains true
, and you're appending that to the file (actually you're appending 1
to the file, since you're converting true
to a string when you write it).