简体   繁体   中英

PHP zip from file

Attempting to create a kmz file from a kml file. The script was working last week, but somehow it stopped working. I am attempting to troubleshoot this issue by creating a .zip file which contains a .kml file.

The process is as follows 1. Kml file is created 2. .zip file is added 3. kml file is added to zip according to name 4. .zip is saved

The issue is that when I open the directory the .zip contains that looks like the file structure of the .kml file, not just the file.

For example if the .kml was inside c:/foldera/folderb. The zip would contain c:/foldera/folderb/kml.

$zip = new ZipArchive();
$zip_name = $_SERVER['DOCUMENT_ROOT'] .'/assets/kml'. "/r".$r.".zip";
$filename = $_SERVER['DOCUMENT_ROOT'] .'/assets/kml'. "/r".$r.".kml";
$zip->open($zip_name, ZIPARCHIVE::CREATE);
$zip->addFile($filename);
$zip->close();
unlink($_SERVER['DOCUMENT_ROOT'] .'/assets/kml'. "/r".$r.".kml");

For some reason you have two addfile's:

$zip->addFile($filename, ltrim($filename, '/'));
$zip->addFile($filename);

Try replacing those two lines with this:

 $zip->addFile($filename, basename($filename));

basename() returns only the filename, removing the path - http://www.php.net/manual/en/function.basename.php

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