note 128805 added to ziparchive.addfile
| From: | Sean at main dot php dot net | Date: | Tue, 15 Aug 2023 20:10:06 +0000 |
| Subject: | note 128805 added to ziparchive.addfile | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-220766@lists.php.net to get a copy of this message | ||
Bad code: You get "Failed to add bar.txt: No error"
<?php
mkdir('/tmp/sample', 0600, true);
$zip = new \ZipArchive();
if (!$zip->open('/tmp/sample/foo.zip', \ZipArchive::CREATE)) {
throw new \RuntimeException('Failed to create zip: ' . $zip->getStatusString());
}
file_put_contents('/tmp/bar.txt', 'bar');
if (!$zip->addFile('/tmp/bar.txt', 'bar.txt')) {
throw new \RuntimeException('Failed to add bar.txt: ' . $zip->getStatusString());
}
?>
Better code: 0700 perms, plus make sure you can write to the zip path first - it doesn't fail
on ZipArchive::open
<?php
mkdir('/tmp/sample', 0700, true);
$zip = new \ZipArchive();
if (!touch('/tmp/sample/foo.zip')) {
throw new \RuntimeException('Cannot write to zip path! Check perms.');
}
if (!$zip->open('/tmp/sample/foo.zip', \ZipArchive::CREATE | \ZipArchive::OVERWRITE)) {
throw new \RuntimeException('Failed to create zip: ' . $zip->getStatusString());
}
file_put_contents('/tmp/bar.txt', 'bar');
if (!$zip->addFile('/tmp/bar.txt', 'bar.txt')) {
throw new \RuntimeException('Failed to add bar.txt: ' . $zip->getStatusString());
}
?>
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.157.134 (proxied: 99.199.75.243)
----
Manual Page -- https://php.net/manual/en/ziparchive.addfile.php
Edit -- https://main.php.net/note/edit/128805
Del: integrated -- https://main.php.net/note/delete/128805/integrated
Del: useless -- https://main.php.net/note/delete/128805/useless
Del: bad code -- https://main.php.net/note/delete/128805/bad+code
Del: spam -- https://main.php.net/note/delete/128805/spam
Del: non-english -- https://main.php.net/note/delete/128805/non-english
Del: in docs -- https://main.php.net/note/delete/128805/in+docs
Del: other reasons-- https://main.php.net/note/delete/128805
Reject -- https://main.php.net/note/reject/128805
Search -- https://main.php.net/manage/user-notes.php