简体   繁体   中英

PHP extract from zip one file at a time

I am using $zip->extractTo() to extract my zip file, this creates a new folder with all the files in it, I am just wondering if there is away I can extract the files one at a time and have them inside the root folder and not create a folder for me?

$zip = new ZipArchive();
if ($zip->open($path) === true) {
    $zip->extractTo('../images/galleries/' . $galleryName);
    $zip->close();
}

You can use the getFromName or getFromIndex methods to extract content from individual files, and then write to disk. For example:

$zip = new ZipArchive;
$zip_filepath = '/path/to/zip/file';
$target_filepath = 'path/to/file/inside/zip';
$dest_filepath = '/path/to/extracted/file';

if ($zip->open($zip_filepath) === TRUE) {
  file_put_contents( $dest_filepath , $zip->getFromName( $target_filepath ) );
  $zip->close();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM