Search code examples
phpzipphp-ziparchive

Extract part of zip file with PHP


For a project, I would like to read a specific file in an uploaded archive (zip file). The file always has the same name/relative-path.

Is it possible to only extract this file, or should I extract the entire archive?

Thanks


Solution

  • $files = array('/a_path/titi.txt'); //create a list of files to be extracted here
    
    $zip = new ZipArchive;
    $res = $zip->open('test_im.zip');
    if ($res === TRUE) {
        $zip->extractTo('/my/destination/dir/', $files);
        $zip->close();
        echo 'ok';
    } else {
        echo 'failed';
    }