Reputation: 1025
I'm completely at a loss for explaining why this isn't working. HELP!
$archive = "x.zip";
$zip = new ZipArchive();
$res = $zip->open($archive);
if ($res === 'TRUE') {
$unzip_success= $zip->extractTo('/temp/', "inscriptions.txt")
$zip->close();
}
Upvotes: 6
Views: 31013
Reputation: 1172
I had the same problem on Windows 10. The only solution I found was just to try extractTo twice, even though the open() was successful:
$zip = new ZipArchive;
if ($open === true) {
$result = $zip->extractTo($destination);
if ($result === false) {
$result = $zip->extractTo($destination);
}
$zip->close();
}
The fact that the second extractTo() works (with no intervening action) would seem to indicate that there's nothing wrong with the archive or the destination directory.
Upvotes: -2
Reputation: 633
I meet same problem, but I can open the zip file, it returns true
after open.
My issue is I got false after $zip->extractTo()
.
I finally success after delete files named in CHINESE (NO-ENGILISH) in the zip file.
Upvotes: 1
Reputation: 309
I faced the same problem, I have fixed this :)
Use $_SERVER['DOCUMENT_ROOT']
for url.
My code (codeigniter):
$this->load->library('unzip');
$file = $this->input->GET('file');
$this->unzip->extract($_SERVER['DOCUMENT_ROOT'].'/TRAS/application/uploads/' . $file,$_SERVER['DOCUMENT_ROOT'].'/TRAS/application/views/templates/' . $file);
Upvotes: 2
Reputation: 1253
Adding document root is what worked for me as well. here is my code
$zip = new ZipArchive;
if ($zip->open($_SERVER['DOCUMENT_ROOT'].'/'.$folder.$file_path) === TRUE) {
$zip->extractTo($_SERVER['DOCUMENT_ROOT'].'/$folder');
$zip->close();
echo 'ok';
}
Upvotes: 1
Reputation: 21
ZipArcive::extractTo is case-sensitive. If file's name to be extracted do not meet zipped one exactly, the method returns false.
Upvotes: 2
Reputation: 536
if nothing works then check if your server is linux. if its linux you can run unzip command to unzip your file via php's system/exec function. i.e
system("unzip archive.zip");
to extract specific file you can check man docs for unzip. many times due to server parameters zip library doesn't work as expected in that cases i switch back to linux commands.
Upvotes: 5
Reputation: 3273
If $res
is equal to 11, that means that ZipArchive
can't open the specified file.
To test this:
$archive = "x.zip";
$zip = new ZipArchive();
$res = $zip->open($archive);
if($res == ZipArchive::ER_OPEN){
echo "Unable to open $archive\n";
}
Upvotes: 1
Reputation: 9586
The problem is that you are quoting TRUE
, which is a keyword and should be left without single quotes. Plus, you could check if the file exists in the zip archive prior to its extraction with locateName:
$archive = "x.zip";
$zip = new ZipArchive();
$res = $zip->open($archive);
if ($res === true && $zip->locateName('inscriptions.txt') !== false) {
$unzip_success= $zip->extractTo('/tmp/', "inscriptions.txt");
$zip->close();
}
Upvotes: 3