note 129446 added to ziparchive.addemptydir
| From: | lea.gris@noiraude.net | Date: | Tue, 30 Apr 2024 09:02:51 +0000 |
| Subject: | note 129446 added to ziparchive.addemptydir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-221311@lists.php.net to get a copy of this message | ||
The <code>addEmptyDir</code> method does not handle permissions bits and directory
<code>mtime</code>. These need to be set separately after the directory has been created
in the Zip Archive.
Note that setting the <code>mtime</code> is only available since PHP >= 8.0, so it
will stay the current timestamp of the <code>addEmptyDir</code> call with PHP 7.4 and
below.
Here is a function to fix both permissions and <code>mtime</code> of a directory in a
Zip Archive.
<pre><code lang="php">
function fixDirAttributes(ZipArchive $zip, string $path, string $name)
{
// Gets the index of the directory in the Zip archive
$indexInZip = $zip->locateName('/' === mb_substr($name, -1) ? $name : $name .
'/');
if (false !== $indexInZip) { // Name found
if (method_exists($zip, 'setMtimeIndex')) { // PHP >= 8.0.0, PECL
zip >= 1.16.0
$zip->setMtimeIndex($indexInZip, filemtime($path)); // Fixes the mtime of the
directory
}
$filePerms = fileperms($path); // Gets permissions on the directory
if (false !== $filePerms) { // filePerms supported
$attr = $filePerms << 16; // Left-shift permission to git the external attributes
value
$zip->setExternalAttributesIndex($indexInZip, \ZipArchive::OPSYS_UNIX, $attr);
}
}
}
</pre></code>
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.13.131 (proxied: 2a0c:e303:0:1d00:2ef0:5dff:fee7:9bd0)
----
Manual Page -- https://php.net/manual/en/ziparchive.addemptydir.php
Edit -- https://main.php.net/note/edit/129446
Del: integrated -- https://main.php.net/note/delete/129446/integrated
Del: useless -- https://main.php.net/note/delete/129446/useless
Del: bad code -- https://main.php.net/note/delete/129446/bad+code
Del: spam -- https://main.php.net/note/delete/129446/spam
Del: non-english -- https://main.php.net/note/delete/129446/non-english
Del: in docs -- https://main.php.net/note/delete/129446/in+docs
Del: other reasons-- https://main.php.net/note/delete/129446
Reject -- https://main.php.net/note/reject/129446
Search -- https://main.php.net/manage/user-notes.php