Search code examples
phplaravellaravel-4zipphp-ziparchive

Object of class ZipArchive could not be converted to string


I'm trying to zip files selected by the user but for some reason I'm getting this error.

I'm using laravel 4.2

Here is the portion of code where the error is originating from.

$zip_name = 'dris-sweet-16-pictures.zip';
$zip = new ZipArchive();
$zip->open($zip_name, ZipArchive::CREATE);
foreach ($lg_img as $img) {
    $zip->addFile(public_path().'/'.$img);                       
}

Response::download($zip, $zip_name, array(
    'content-type'          => 'application/zip',
    'Content-disposition:'  =>  'attachment; filename=filename.zip',
    'Content-Length:'       => filesize($zip_name)
));

$zip->close();

Solution

  • As described here : http://laravel.com/docs/responses#special-responses, the arguments for this method are in the following order :

    Response::download($pathToFile, $name, $headers);
    

    In your code, you are passing $zip, your ZipArchive as the path to the file but the method expect here a string, so the path to your file on the disk.

    Cheers.