Download a zip file


#1

Hello, I need to download a zip file.

$file = tempnam(sys_get_temp_dir(), “TMP0”);
$filesystemZip = new Filesystem(new ZipArchiveAdapter($file));
foreach ($contents as $object) {
$file_path = $object[‘path’];
$contentsP = $filesystem->read($file_path);
$filesystemZip->put($file_path, $contentsP);
}
$filesystemZip->getAdapter()->getArchive()->close();

$fh = fopen($file, ‘rb’);
$stream = new \Slim\Http\Stream($fh); // create a stream instance for the response body

unlink($file);

$response = $response->withHeader(‘Content-Type’, ‘application/zip’);
//$response = $response->withHeader(‘Content-Type’, ‘application/octet-stream’);
//$response = $response->withHeader(‘Content-Type’, ‘application/download’);
// $response = $response ->withHeader(‘Content-Description’, ‘File Transfer’);
//$response = $response ->withHeader(‘Content-Transfer-Encoding’, ‘Binary’);
$response = $response ->withHeader(‘Content-Disposition’, ‘attachment; filename= test.zip’);
//$response = $response->withHeader(‘Expires’, ‘0’);
//$response = $response->withHeader(‘Cache-Control’, ‘must-revalidate, post-check=0, pre-check=0’);
//$response = $response->withHeader(‘Pragma’, ‘no-cache’);
//$response = $response->withHeader(‘Content-length’,$stream->getSize());
//$response = $response->withBody($stream);
return $response->withBody($stream);
}

the problem is that the file has a first caracter ‘/t’ and I dont no way.

I use linux and centos 7.

Help please.


#2

solved

@ob_start(’’); //@ supresses a warning
//header entries
ob_end_clean();
ob_clean();

$fh = fopen($file, “rb”);
$stream = new \Slim\Http\Stream($fh); // create a stream instance for the response body

unlink($file);


#3

This doesn’t feel like a problem in Slim. You probably have extraneous whitespace somewhere in your source files. Double-check that all files start with <?php and get rid of ?> whenever unnecessary.

If you can’t find it, try headers_sent() to find the exact location.