PHP zip_read - Syntax
zip_read(resource $zip): resource|false
zip_read() advances to the next directory entry in an open ZIP archive. PHP deprecated this procedural function in 8.0. For new code, use ZipArchive::getNameIndex().
zip_read()
Use ZipArchive to read entries by name in modern PHP.
To run the examples, save bbminfo.zip and bbminfo-nested-folder.zip beside your script and run it from that directory.
zip_read(resource $zip): resource|false
| Arguments | resource $zip |
|---|---|
| Return value | An entry resource, or false when no next entry is available. |
A classroom kiosk builds a list of resources inside a downloaded ZIP. Advance the reader until it reaches the end of the available members.
<?php
class ZipExample {
public function processZipArchieve() {
$zipHandler = zip_open("./bbminfo.zip");
if(is_resource($zipHandler)) {
while($zipEntry = zip_read($zipHandler)) {
$fileName = zip_entry_name($zipEntry);
echo("File Name: " . $fileName);
echo "\n" ;
}
zip_close($zipHandler);
} else {
echo("Failed to Open. Error Code: " . $zipHandler);
}
}
}
$obj = new ZipExample();
$obj->processZipArchieve();
File Name: readme.txt
A librarian catalogues a resource pack that includes nested folders. Inspect each returned entry before moving the reader to the next member.
<?php
class ZipExample {
public function processZipArchieve() {
$zipHandler = zip_open("./bbminfo-nested-folder.zip");
if(is_resource($zipHandler)) {
while($zipEntry = zip_read($zipHandler)) {
/* Opening a File in a Zip Archive */
$flag = zip_entry_open($zipHandler, $zipEntry, "rb");
if ($flag == true) {
$resourceName = zip_entry_name($zipEntry);
if(substr($resourceName, -1) == "/") {
echo("Directory: " . $resourceName . " opened Successfully. \n");
} else {
echo("File: " . $resourceName . " opened Successfully. \n");
}
} else {
echo("Failed to Open. \n");
}
}
zip_close($zipHandler);
} else {
echo("Failed to Open. Error Code: " . $zipHandler);
}
}
}
$obj = new ZipExample();
$obj->processZipArchieve();
Directory: bbminfo-nested-folder/img/ opened Successfully. File: bbminfo-nested-folder/img/bbminfo_icon.png opened Successfully. File: bbminfo-nested-folder/readme.txt opened Successfully. File: bbminfo-nested-folder/tutorial-doc.pdf opened Successfully.
A missing course upload leaves the opening step without a valid archive resource. The original diagnostic shows why the importer must check the opening result before it calls the entry reader.
<?php
class ZipExample {
public function processZipArchieve() {
/* Invalid zip archieve or an invalid file path */
$zipHandler = zip_open("./invaid.zip");
while($zipEntry = zip_read($zipHandler)) {
$fileName = zip_entry_name($zipEntry);
echo("File Name: " . $fileName);
echo "\n" ;
}
}
}
$obj = new ZipExample();
$obj->processZipArchieve();
Warning: zip_read() expects parameter 1 to be resource, int given in ~\zip-example.php on line 9
Reference: PHP manual — zip_read()