BBM Tutor
BBM Tutor

PHP Zip - zip_read()

Description & Uses of PHP zip_read()

The PHP zip_read() function is used to read an entity (a file or a directory) present in an opened Zip Archive Recsource Handler. It returns the Sub Resource Handler of a single entity present in a Main Resource Handler only if Success otherwise a PHP Warning.

Simple Example - PHP zip_read(Resource $ZipHandler);

PHP Input Screen

<?php
$ZipHandler = zip_open("./YGen.zip");
/* Zip Archive's Resource Handler Reading Operation */
while($zipEntry = zip_read($ZipHandler)) {
$file_Name = zip_entry_name($zipEntry);
echo("File Name: " . $file_Name);
echo "<br>" ;
}
zip_close($ZipHandler);
?>

PHP Output Screen

File Name: YGen.jpg
File Name: YGen.pdf
File Name: YGen.txt

bbminfo Demo Zip Archive File: YGen.zip

  • It contains 3 Files, they are
    • YGen.jpg
    • YGen.pdf
    • YGen.txt
  • Download the Demo Zip Archive File: YGen.zip for your tutorial reference purpose.
BBM Invoice

Simple PHP Syntax

<?php
zip_read(Resource $ZipHandler);
?>

Parameter & Return Type

Description

Resource $ziphandler
Specifies the Resource Handler of an already opened Zip Archive File. (*Required)
Return Mixed $zipEntry
Returns the Sub Resource Handler of a single entity present in a Main Resource Handler otherwise a PHP Warning.

What is PHP zip_read() ?

  • It reads the entries in the specified Zip Archive and it returns the Resource Handler of the entries for further Zip Entry operations.
  • It returns the Sub Resource Handler of a single entity present in a Main Resource Handler only if Success otherwise a PHP Warning.
  • The Zip Archive must contain atleast a file or a directory otherwise it produce a PHP Warning (Refer Example #2)
  • Reading of an Invalid or an Empty Resource Handler leads to PHP Warning (Refer Example #2 & #3). It can be avoided by a special Error Handling Technique (Refer Example #4)
BBM Invoice

Example #1 - PHP zip_read(Resource $ZipHandler);

PHP Input Screen

<?php
$ZipHandler = zip_open("./BBMINFO.zip");
if(is_resource($ZipHandler)) {
/* Zip Archive's Resource Handler Reading Operation */
while($zipEntry = zip_read($ZipHandler)) {
$file_Name = zip_entry_name($zipEntry);
echo("File Name: " . $file_Name);
echo "<br>" ;
}
zip_close($ZipHandler);
} else {
echo("Failed to Open. Error Code: " . $ZipHandler);
}
?>

PHP Output Screen

File Name: YGen/YGen.jpg
File Name: YGen/YGen.pdf
File Name: YGen/YGen.txt
File Name: BBMINFO.jpg
File Name: Invoice/Features/
File Name: Invoice/Features/KeyFeatures.rtf
File Name: Invoice/Invoice.txt

bbminfo Demo Zip Archive File: BBMINFO.zip

  • It contains 2 Directories & 1 File, they are
    • Directory : YGen contains 3 Files, they are
      • YGen.jpg
      • YGen.pdf
      • YGen.txt
    • Directory : Invoice contains 1 Sub Directory & 1 File
      • Directory : Features contains 1 File
        • KeyFeatures.rtf
      • Invoice.txt
    • BBMINFO.jpg
  • Download the Demo Zip Archive File: BBMINFO.zip for your tutorial reference purpose.
BBM Invoice

Example #2 - PHP zip_read(Resource $ZipHandler);

PHP Input Screen

<?php
/* Empty Zip FIle - 0 Files in the Zip FIle */
$ZipHandler = zip_open("./EmptyBox.zip");
if(is_resource($ZipHandler)) {
/* Zip Archive's Resource Handler Reading Operation */
while($zipEntry = zip_read($ZipHandler)) {
$file_Name = zip_entry_name($zipEntry);
echo("File Name: " . $file_Name);
echo "<br>" ;
}
zip_close($ZipHandler);
} else {
echo("Failed to Open. Error Code: " . $ZipHandler);
}
?>

PHP Output Screen

PHP Warning: zip_read() expects parameter 1 to be resource, integer given in ../Tutor/error.php on line 5

bbminfo Demo Zip Archive File: EmptyBox.zip

  • It contains 0 Files (No Files are there in the Zip Archive)
  • Download the Demo Zip Archive File: EmptyBox.zip for your tutorial reference purpose.
BBM Invoice

Example #3 - PHP zip_read(Resource $ZipHandler);

PHP Input Screen

<?php
/* External Link Representation - Invalid Link */
$ZipHandler = zip_open("https://www.bbminfo.com/Tutor/YGen.zip");
if(is_resource($ZipHandler)) {

/* Zip Archive's Resource Handler Reading Operation */
while($zipEntry = zip_read($ZipHandler)) {
$file_Name = zip_entry_name($zipEntry);
echo("File Name: " . $file_Name);
echo "<br>" ;
}
zip_close($ZipHandler);
} else {
echo("Failed to Open. Error Code: " . $ZipHandler);
}
?>

PHP Output Screen

PHP Warning: zip_read() expects parameter 1 to be resource, integer given in ../Tutor/error.php on line 5

BBM Invoice

Example #4 - PHP zip_read(Resource $ZipHandler);

PHP Input Screen

<?php
/* External Link Representation - Invalid Link */
$ZipHandler = zip_open("https://www.bbminfo.com/Tutor/YGen.zip");
if(is_resource($ZipHandler)) {
/* Zip Archive's Resource Handler Reading Operation */
while($zipEntry = zip_read($ZipHandler)) {
$file_Name = zip_entry_name($zipEntry);
echo("File Name: " . $file_Name);
echo "<br>" ;
}
zip_close($ZipHandler);
} else {
echo("Failed to Open. Error Code: " . $ZipHandler);
}
?>

PHP Output Screen

Failed to Open. Error Code: 11

BBM Invoice
 
Disclaimer: Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy.