PHP ZipArchive deleteName() Function
Last Updated :
23 Mar, 2023
The ZipArchive::deleteName() function is an inbuilt function in PHP that is used to delete an entry from the zip archive using its name.
Syntax:
bool ZipArchive::deleteName(string $name)
Parameters: This function accepts a single parameter that is described below.
- $name: This parameter holds the file name that you want to delete from the zip archive.
Return Value: This function returns “true” on success and “false” on failure.
Example 1: The following code demonstrates the deleteName() function.
PHP
<?php
$zip = new ZipArchive;
if ( $zip ->open( 'Geeks.zip' , ZipArchive::CREATE))
{
if ( $zip ->deleteName( 'GFG2.txt' )) {
echo 'File deleted successfully' ;
} else {
echo 'File not deleted' ;
}
$zip ->close();
}
else
{
echo 'Failed to open zip file' ;
}
?>
|
Output:
Example 2: The following code demonstrates the deleteName() function with the following files.
PHP
<?php
$zip = new ZipArchive;
if ( $zip ->open( 'Geeks.zip' , ZipArchive::CREATE)) {
$zip ->addFromString(
'GFG1.txt' ,
'Welcome to GeeksforGeeks'
);
$zip ->addFromString(
'GFG2.txt' ,
'A computer science portal'
);
$zip ->addFromString(
'GFG3.txt' ,
'Welcome to GeeksforGeeks'
);
if ( $zip ->deleteName( 'GFG3.txt' )) {
echo 'File deleted successfully' ;
} else {
echo 'File not deleted' ;
}
$zip ->close();
}
else
{
echo 'Failed to open zip file' ;
}
?>
|
Output:
Reference: https://www.php.net/manual/en/ziparchive.deletename.php
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...