Fellow coders:
Hate to ask this question, as it was, I belive correctly answered two times:
Here
And here
So the problem is: when unzipping the downloaded 'file.zip', after unzipping, it contains all the folders from the path, even tough I specified the local file in the addFile function. I'd like the zip file not to contain any subfolder, just files.
$zipname = str_replace(' ', '_', $obj['Name']) . '.zip';
$zip = new ZipArchive();
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
// $file looks like this: webroot/uploadify/files/file.jpg
$zip->addFile($file, pathinfo($file, PATHINFO_BASENAME));
}
$zip->close();
header('Content-Description: File Transfer');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=" . $zipname);
header("Pragma: no-cache");
header("Expires: 0");
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
Can anyone see what's the problem with this code?
Additional info:
I, ofc, use paths relative to the webroot, but the folder hierarchy in the extracted folder after: extract to file -> 'my_file.zip' is C->xampp->htdocs->my_cakephp_web_app->app->webroot->files which is not exactly what I wanted to achieve :)
PHP 5.4.4
Windows XP, XAMPP
EDIT
Tried using the http://www.phpconcept.net/pclzip/ library and had the same problem. Also on my home PC ( win7 ) this code worked well.
This brings up a question: Are there any settings I should meet that I'm not aware of?
I have run your code and it works as expected. So there are 3 things that could be going wrong from what I can see.
Try using these flags in case an old file still exists and it is not being overwritten (it is not clear what $obj['Name'] is set to ) If you read the comments in the php manual, there are varying degrees of success using ZIPARCHIVE::CREATE vs ZIPARCHIVE::OVERWRITE, so perhaps using both will work.
$zip->open( $zipname, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE );
Pathinfo could be failing and returning the whole path or null for some reason
Is it possible to do a var_dump on the pathinfo output, just to confirm it is what you expect it to be?
foreach ( $files as $file )
{
var_dump(pathinfo( $file, PATHINFO_BASENAME ));
// $file looks like this: webroot/uploadify/files/file.jpg
// $zip->addFile( $file, pathinfo( $file, PATHINFO_BASENAME ) );
}
Maybe there is a bug with your os/php version with addFile function (unlikely, grasping at straws here as i can't see any other explanation)
In PHP 5.3 this should do it:
$phar = new PharData('file.zip');
// add all files in the folder, only include files with extensions
$phar->buildFromDirectory('/directory', '~[.][0-9a-z]+$~i');
You could also use PharData::buildFromIterator
with the LEAVES_ONLY
flag.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With