Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Zip Archive - open or extractTo is not working

Tags:

php

zip

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();
}
  • the target dir "temp" is "0777" permissions
  • the code obtained from $res is "11" and not "TRUE" as required by the documentation on PHP.net
  • note: must put the full url for $archive and the first argument of extractTo
like image 868
Lawrence DeSouza Avatar asked Jul 09 '12 19:07

Lawrence DeSouza


2 Answers

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.

like image 165
Hardik Avatar answered Oct 26 '22 08:10

Hardik


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();
}
like image 38
Diego Agulló Avatar answered Oct 26 '22 10:10

Diego Agulló