PHP zip_open - Syntax
zip_open(string $filePath): mixed
The PHP zip_open() function is used to open the specified Zip Archive file. It creates a new stream and establishes a connection between the stream and a Zip Archive and returns a Resource Handler for the newly created stream only if the Zip Archive File is a Valid Link otherwise an error code.
zip_open(string $filePath): mixed
string $filePath |
The File Path of the specified zip archive file to be open and the file name should be case sensitive (*Required) |
---|---|
Return Mixed $zipHandler |
Returns a resource handler if Success otherwise an error. |
The Common Error Codes in PHP Zip Functions are
Note: The PHP zip_open() will support only the .zip extension file. It won't process the third parties archieves for example .rar, .7z, etc.
Open the zip archieve using zip_open library method, once the method returns the zip handler then read the files and print their names.
<?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();
Open the zip archieve from a remote URL using zip_open library method. Its only accept the Internal Link Representation of a Specifed zip archive file like (./bbminfo.zip) otherwise it returns an Error Code : 11 on an external Link Representation of a same zip archive file like (https://www.bbminfo.com/php/library/zip/bbminfo.zip) or an invalid File path.
<?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();
Try to open a zip archieve file name Bbminfo.zip instead of a correct file bbminfo.zip, it will return an error code 11 because the file name should be a case sensitive.
<?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();
Try to open a empty zip archieve file (i.e. the zip archieve contains no files), it will return an error code 19 because the zip archieve should contain atleast a file.
<?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();