PHP ZipArchive is not adding any files (Windows)

phpzipfilesziparchivefilewindows

Last Update : 2023-09-22 UTC 08:16:26 AM

Answers of > PHP ZipArchive is not adding any files (Windows)

w3coded php windows Stack Overflow for Teams w3coded php windows Where developers & technologists w3coded php windows share private knowledge with coworkers w3coded php windows , w3coded php windows Stack w3coded php windows Overflow Public w3coded php windows questions & answers w3coded php windows ,Thanks for contributing an w3coded php windows answer to Stack Overflow!,PHP documentation: w3coded php windows ZipArchive::addFile

PHP documentation: ZipArchive::addFile

$zip->addFile(
    $fileToZip, 
    basename($fileToZip)
);

Also need to change destination where script try to find hello.txt file $zip->addFile($fileToZip, basename($fileToZip))

<?php

$destination = __DIR__.'/tmp/makeZipTest.zip';
$fileToZip = __DIR__.'/hello.txt';

$zip = new ZipArchive();
if (true !== $zip->open($destination, ZipArchive::OVERWRITE)) {
  die("Problem opening zip $destination");
}
if (!$zip->addFile($fileToZip, basename($fileToZip))) {
  die("Could not add file $fileToZip");
}
echo "numfiles: " . $zip->numFiles . "\n";
echo "status: " . $zip->status . "\n";
$zip->close()

check this class to add files and sub-directories in a folder to zip file,and also check the folder permissions before running the code, i.e chmod 777 -R zipdir/

HZip::zipDir('/path/to/sourceDir', '/path/to/out.zip'); 

<?php 
class HZip 
{ 

private static function folderToZip($folder, &$zipFile, $exclusiveLength) { 
$handle = opendir($folder); 
while (false !== $f = readdir($handle)) { 
  if ($f != '.' && $f != '..') { 
    $filePath = "$folder/$f"; 
    // Remove prefix from file path before add to zip. 
    $localPath = substr($filePath, $exclusiveLength); 
    if (is_file($filePath)) { 
      $zipFile->addFile($filePath, $localPath); 
    } elseif (is_dir($filePath)) { 
      // Add sub-directory. 
      $zipFile->addEmptyDir($localPath); 
      self::folderToZip($filePath, $zipFile, $exclusiveLength); 
    } 
  } 
} 
closedir($handle); 
} 


public static function zipDir($sourcePath, $outZipPath) 
{ 
$pathInfo = pathInfo($sourcePath); 
$parentPath = $pathInfo['dirname']; 
$dirName = $pathInfo['basename']; 

$z = new ZipArchive(); 
$z->open($outZipPath, ZIPARCHIVE::CREATE); 
$z->addEmptyDir($dirName); 
self::folderToZip($sourcePath, $z, strlen("$parentPath/")); 
$z->close(); 
} 
} 

Current topics : PHP ZipArchive is not adding any files (Windows)

Newly Added Questions

Similar Questions

Questions :

How To Group Array Key Value

Last Update : 2023-09-22 UTC 12:43:37 PM

Questions :

PhpStorm Warning For React Attributes In Jsx File With SCSS File

Last Update : 2023-09-22 UTC 12:43:23 PM

Questions :

Why Is The File Not Showing Up In Request.files And In Request.forms Instead?

Last Update : 2023-09-22 UTC 12:43:16 PM

Questions :

Proxying Assets From React App Directory In Slim Framework?

Last Update : 2023-09-22 UTC 12:43:02 PM

Questions :

Laravel 5.4 Can't Run “php Artisan Preset React” Comand

Last Update : 2023-09-22 UTC 12:42:42 PM

Questions :

How To Update Session Values Without Signing Out?

Last Update : 2023-09-22 UTC 12:42:30 PM

Questions :

Array Is Not Visible

Last Update : 2023-09-22 UTC 12:42:20 PM

Questions :

React Routing For Login Using Symfony

Last Update : 2023-09-22 UTC 12:42:03 PM

Questions :

Sanctum With React SPA Returning 419 Page Expired

Last Update : 2023-09-22 UTC 12:41:47 PM

Questions :

How Do I Import An Input String Into Another Page

Last Update : 2023-09-22 UTC 12:41:33 PM

Top
© 2023 W3CODED - All Rights Reserved.