How to check if file exist in zip archive

11,778

Solution 1

It should be like this:

$zip = new ZipArchive();
if($zip->open('test.zip') === TRUE )
{
    if ($zip->locateName('moduleConfig.xml') !== false)
    {
    echo "Config exists";
    }
}
else {
    echo 'Failed code:'. $res;
}

Solution 2

try:

if ($zip->locateName('moduleConfig.xml') !== false)
{
    echo "Config exists";
}
Share:
11,778
Ivan
Author by

Ivan

PHP Developer

Updated on June 20, 2022

Comments

  • Ivan
    Ivan almost 2 years

    i have zip archive and after extract him i need to check if moduleConfig.xml exist inside zip archive. How i can do that.

    I try this

    $zip = new ZipArchive();
    if($zip->open('test.zip') === TRUE )
    {
        if(file_exists($zip->getFromName('moduleConfig.xml')))
        {
            echo "Config exists";
            // Do somthing
        }
    }
    else {
        echo 'Failed code:'. $res;
    }
    
    • dbf
      dbf about 11 years
      you tried this, but it did what instead?
    • Ivan
      Ivan about 11 years
      I try this and this dont work. I dont understand ur question.
  • Hector
    Hector over 7 years
    Note: You should check it using !== false because it can return 0 when file exists. so if ( $zip->locateName('file.xml') ) {...} doesn't works correctly. more info