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();
}
}
Last Update : 2023-09-22 UTC 12:43:23 PM
Last Update : 2023-09-22 UTC 12:43:16 PM
Last Update : 2023-09-22 UTC 12:43:02 PM
Last Update : 2023-09-22 UTC 12:42:42 PM
Last Update : 2023-09-22 UTC 12:42:30 PM
Last Update : 2023-09-22 UTC 12:41:47 PM
Last Update : 2023-09-22 UTC 12:41:33 PM