1

In CakePHP controller I have the logic to generate a ZIP which is as follows and works perfectly fine, I can find the ZIP archive on the server:

function getAttachmentsInZip( $meetingId = null ){
    $this->autoRender = false;
    $this->request->allowMethod( ['post'] );
    $allTasksWithFiles_query = $this->Tasks->find('all')
        ->where(['Tasks.uploaded_file_path != ' => 'NULL']);
    $allTasksWithFiles = $allTasksWithFiles_query->toArray();
    $files = array();
    foreach( $allTasksWithFiles as $taskWithFile ){
        $files[] = $taskWithFile['uploaded_file_path'];
    }

    if( !empty( $files ) ){
        $destination = 'uploads/archives/meeting_' . $meetingId .'.zip';
        $zip = new ZipArchive();
        $zip->open( $destination, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE );

        foreach( $files as $file ){
            $zip->addFile('uploads/uploaded_files/' . $file, $file);
        }
        $zip->close();
    }
}

However, I am completely unable to return the archive straight to user's browser. The closest I got was the following snippet, but the archive is broken and cannot be opened:

header("Content-type: application/zip");
header('Content-Disposition: attachment; filename="' . $destination . '"');
header("Pragma: no-cache");
header("Expires: 0");
readfile("asd");

Any help or guidance is much appreciated. If I should have/could have provided more code — please ask.

Antti29
  • 2,953
  • 12
  • 34
  • 36
WpDoe
  • 476
  • 1
  • 7
  • 22
  • 1
    Do not manually use header, use the response object methods to set headers. See http://book.cakephp.org/3.0/en/controllers/request-response.html#sending-files and following. – mark Mar 31 '16 at 12:38
  • 1
    Thanks a lot, that did help and I managed to get the work done. – WpDoe Apr 01 '16 at 10:04

0 Answers0