I have this code for my site, the zip is generating (but corrupted because of the 0 byte files...) but the actual curled files (in the tmp/ directory) are there, just with nothing in them.

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);

        }

I was thinking of possibly not using curl to write to the file and instead writing the data to the file via php's inbuilt file interaction functions. How would I go about doing this?

Recommended Answers

All 7 Replies

I have some extra information:
exif_imagetype returns 2 (a jpeg) which was expected
My mac's get info pane correctly renders a preview but does not recognise the file as an image.
08694d342b01eaf59dad214eeb67c2f9

EDIT: This happens with both the file in the tmp/ directory but also the unzipped file.

Set the handler to curl_init() function, I did a test and by serving the file like this, the first part of the script worked fine:

$url = "file://{$_SERVER['DOCUMENT_ROOT']}/{$filename}";

Now it depends on the contents of $row['loc'], if you're already including the full path, then just add file:// or http:// if this is remote.

The file is succesfully downloading, and it contains the content of an image file - it just isn't being recognised as an image file by my computer.

I don't have much experience on Mac, but I can suggest to perform few tests, the first is a php script to display the signature of the file:

<?php

$f = "./tmp/124file.jpg";
$fp = fopen($f, "r");
$r = fread($fp, 10);
fclose($fp);
echo substr(chunk_split(bin2hex($r), 2, ":"), 0, -1) . PHP_EOL;

If the first 3 bytes are ff:d8:ff:... then it's jpeg. You can run this on server and in the local machine to see if something is corrupting the files. You can also check with an hex editor or with the file command in the Mac OS terminal, the correct command should be this:

file -m mime-type 124file.jpg

Some applications do not use the same methods to determine the mime type, but with the above you should get a correct result.

Here's a list of signatures:

I can only think that some of these files were renamed to jpg and instead are png or gifs. Most browsers can recognize the correct mime and display the file correctly, but the operative systems can act differently.

Hope it helps.

What appears to be happening is the files don't actually exist. ls brings them up but they are green with two question marks after them. Running the file command on them tells me there is no file or directory called x. However Get Info is still showing a preview.

I've put one of the download files here: http://cl.ly/1R2p0i0P1v2u because I have no idea what is actually wrong with the file. I've converted to file_get_contents but to no avail

This is the output of ls

february-13-photoshop_life__86-calendar-1920x1080-1.jpg??*
february-13-photoshop_life__86-calendar-1920x1080-2.jpg??*
february-13-photoshop_life__86-calendar-1920x1080-3.jpg??*
february-13-photoshop_life__86-calendar-1920x1080-4.jpg??*
february-13-photoshop_life__86-calendar-1920x1080.jpg??*

Try with ls -lh it will return the sizes and some other information.

The uploaded file seems regular to me, it is a jpg, this is the output of the ImageMagick identify tool:

identify feb.jpg 
feb.jpg JPEG 1920x1080 1920x1080+0+0 8-bit DirectClass 332KB 0.000u 0:00.019

If you still get empty files in tmp directory, then try to change the $url as I suggested in my previous post, change also:

$path = 'tmp/';

to:

$path = $_SERVER['DOCUMENT_ROOT'].'/tmp/';

To be sure all the directories are correctly identified. When I first tried your code I got empty files too, but changing the $url I fixed that issue. I do not other ideas, sorry!

Tried a code modification that does not write the file to my servers at all and directly streams it into a zip:

if ($_GET['download']== 'true'){
          $zip = new zipfile();
          while($row = mysql_fetch_array($res)){
            // echo $row['loc'];
            // $parsedurl = parse_url($row['loc']);
            // $parsedurl = $parsedurl['string'];
            // $parsedurl = str_replace("https://www.filepicker.io/api/file/","", $parsedurl);
            // echo $parsedurl;
            // $files[] = $parsedurl;
            // echo var_dump($files);
            $url = str_replace('https', 'http', $row['loc']);
            $image = file_get_contents($url . '+' . $row['name']);

            $zip->addFile($image, $row['name']);

            //get the zip content and send it back to the browser


          }
          header("Content-type: application/octet-stream");
          header("Content-Disposition: attachment; filename=photos.zip");
          header("Content-Description: Automated picturecamel download");
          echo $zip->file();
          die();

However, this still does not work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.