To run the examples, save bbminfo.zip beside your script and run it from that directory.
Reading the same archive with ZipArchive
This example requires the PHP ZIP extension.
PHP source code
<?php
$zip = new ZipArchive();
$result = $zip->open(__DIR__ . '/bbminfo.zip', ZipArchive::RDONLY);
if ($result !== true) {
throw new RuntimeException('Could not open the archive.');
}
try {
echo $zip->getNameIndex(0), PHP_EOL;
} finally {
$zip->close();
}
Output
bbminfo/readme.txt
PHP zip_open - Syntax
Syntax
zip_open(string $filename): resource|int|false
PHP zip_open - Parameter and return type
Arguments
string $filename
Return value
A ZIP archive resource on success, or an integer error code or false on failure. Check with is_resource before reading.
Two ZIP error codes you may encounter are listed below. The exact code depends on the failure.
ER_OPEN (11) indicates that the file could not be opened.
ER_NOZIP (19) indicates that the file format is not recognized as ZIP.
ZIP is a file format. Renaming a RAR or 7z file with a .zip extension does not convert it to ZIP.
Example: #1 - PHP zip_open
A teacher has downloaded the class bundle into the same folder as the script. Open that local file and list its contents to confirm the handouts arrived.
Inputbbminfo.zip
ResultFile Name: readme.txt
PHP source code
<?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();
Output
File Name: readme.txt
Advertisements
Example: #2 - PHP zip_open
A student copies a download address into a field intended for a local archive filename. The legacy reader needs the path of the saved file to open it.
<?php
class ZipExample {
public function processZipArchieve() {
/* External Link Representation */
$zipHandler = zip_open("https://www.bbminfo.com/php/library/zip/bbminfo.zip");
if(is_resource($zipHandler)) {
echo("Successfully Zip Archive File Opened.");
zip_close($zipHandler);
} else {
echo("Failed to Open. Error Code: " . $zipHandler);
}
}
}
$obj = new ZipExample();
$obj->processZipArchieve();
Output
Failed to Open. Error Code: 11
Example: #3 - PHP zip_open
An archive worked on one classroom computer and fails after it is moved. Compare the capital letters in the configured filename with the actual name on a case-sensitive filesystem.
InputBbminfo.zip
ResultFailed to Open. Error Code: 11
PHP source code
<?php
class ZipExample {
public function processZipArchieve() {
/* Case Sensitive Mistake: Bbminfo.zip instead of bbminfo.zip */
$zipHandler = zip_open("./Bbminfo.zip");
if(is_resource($zipHandler)) {
echo("Successfully Zip Archive File Opened.");
zip_close($zipHandler);
} else {
echo("Failed to Open. Error Code: " . $zipHandler);
}
}
}
$obj = new ZipExample();
$obj->processZipArchieve();
Output
Failed to Open. Error Code: 11
Example: #4 - PHP zip_open
A submission named empty.zip arrives with no valid ZIP structure. Check the result of opening it before treating it as an archive with zero members.
Inputempty.zip
ResultFailed to Open. Error Code: 19
PHP source code
<?php
class ZipExample {
public function processZipArchieve() {
/* Case Sensitive Mistake: Bbminfo.zip instead of bbminfo.zip */
$zipHandler = zip_open("./empty.zip");
if(is_resource($zipHandler)) {
echo("Successfully Zip Archive File Opened.");
zip_close($zipHandler);
} else {
echo("Failed to Open. Error Code: " . $zipHandler);
}
}
}
$obj = new ZipExample();
$obj->processZipArchieve();