1

I am creating zip file using ZipArchive in php. code-

$path = Yii::getPathOfAlias('webroot').'/quickshopimages/productimage/';
$zip =new ZipArchive();
$zip->open('test.zip', ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE);
$iterator=new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path),
RecursiveIteratorIterator::SELF_FIRST);
foreach($iterator as $file) 
{
  if($file->isDir())   
  {
   echo strtoupper($file->getRealpath()), PHP_EOL.'<br/>';$file .'/');
   $zip->addEmptyDir(str_replace(strtoupper($file->getRealpath()) . '/', '', 
   $file . '/'));
  }
 else
 {
  $zip->addFile($file);
 }
}
$zip->close();

But here problem is that when I create zip its folder structure sets as

C:\Program Files (x86)\EasyPHP-5.3.5.0\www\quickshop\trunk\quickshopimages\productimage\male\shirts\Ethnic\1

and my main root directory is from "productimage\male\shirts\Ethnic\1". So, how can start zip folder from "productimage" as root here?

Thanks in advance and sorry for English

Rohit13
  • 299
  • 4
  • 16

2 Answers2

0

See the answers below, your problem seems to comes from your usage of $zip->addFile($file);, with no second parameter.

php creating zips without path to files inside the zip

Community
  • 1
  • 1
weeger
  • 429
  • 2
  • 9
0

Yii has a zip extension. yii zip ext.

$zip = Yii::app()->zip;
$zip->makeZip('./','./toto.zip'); // make an ZIP archive 
$zip->extractZip('./toto.zip', './1/'); // extract Zip arc.
Max Sherbakov
  • 1,817
  • 16
  • 21