Reputation: 348
I have a zip file called project.zip with the following structure:
project.zip
\project
\file.pdf
I need to delete file.pdf. I tried the following code but I'm getting an error.
Thanks
$zip = new ZipArchive();
$zip_name = 'path\to\project.zip';
$zip->open( $zip_name );
$zip->deleteName( 'project\file.pdf' );
$zip->close();
I Also tried with a leading backslash but with no success,
$zip->deleteName( 'prject\file.pdf' );
Upvotes: 2
Views: 2497
Reputation: 2115
It's weird, but it seems you need to include the base name of the zip file in the filename like this:
$zip->deleteName( 'project/project/file.pdf' );
Try something like this to see what the filename values look like in your zip:
for ($i = 0; $i < $zip->numFiles; $i++) {
$filename = $zip->getNameIndex($i);
echo $filename . "<br>";
}
Also don't forget to close the zip when you are done
$zip->close();
Upvotes: 2