Laura
Laura

Reputation: 11

How extract folder php

I'm trying to extact a zip folder in php, I'm using ZipArchive for do that but it doesn't work.

I check the permission and it's ok I also check the destination and it's ok too

 $zip = new \ZipArchive();
if ($zip->open($pathShapeFile, ZipArchive::CREATE | ZIPARCHIVE::OVERWRITE)) {
   var_dump($zip);
   $path = pathinfo($pathShapeFile);
   $path_tmp = $path['dirname'] . DS . $path['filename'] . '_dezip';
   if(!is_dir($path_tmp)) {
      mkdir($path_tmp);
      chmod($path_tmp, 0777);
   }

   $extract = $zip->extractTo($path_tmp);
}

The folder of destination is created by it's empty

$extract return object(ZipArchive)#515 (5) { ["status"]=> int(0) ["statusSys"]=> int(0) ["numFiles"]=> int(0) ["filename"]=> string(50) "/srv/files/shape/Dossier514.zip.zip" ["comment"]=> string(0) "" } and this line "$zip->open($pathShapeFile, ZipArchive::CREATE | ZIPARCHIVE::OVERWRITE)" return true

Upvotes: 1

Views: 45

Answers (1)

You have an error in your code.

you are creating archive rather than opening it.

$zip->open($pathShapeFile, ZipArchive::CREATE | ZIPARCHIVE::OVERWRITE) //this is for archive creation

so, replace your code with

$pathShapeFile = '/tmp/yourziparchive.zip';
$zip = new \ZipArchive();
if ($zip->open($pathShapeFile)) { // here you open file, no create and owerwrite mask!
   $path = pathinfo($pathShapeFile);
   $path_tmp = "{$path['dirname'] . DS . $path['filename'] . '_dezip'; //here i don't know what is DS, but let's assume that this is your syntax mistake
   if(!is_dir($path_tmp)) {
      mkdir($path_tmp);
      chmod($path_tmp, 0777); //i don't think you need 777 rights on created dir, but it is your choice
   }

   $extract = $zip->extractTo($path_tmp);
}
$zip->close();

i don't know your $pathShapeFile value, but it seems that or there is no full path (maybe relative to script but parsed wrong). So as you see in my example you have to use absolute path.

Upvotes: 1

Related Questions