Mosaic
Mosaic

Reputation: 348

Delete file from directory inside zip

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

Answers (2)

bitfiddler
bitfiddler

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

hek2mgl
hek2mgl

Reputation: 157947

use forward slashes :

$zip->deleteName( 'project/file.pdf' ); 

Upvotes: 1

Related Questions