简体   繁体   English

来自文件的PHP zip

[英]PHP zip from file

Attempting to create a kmz file from a kml file. 尝试从kml文件创建kmz文件。 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. 我正在尝试通过创建一个包含.kml文件的.zip文件来解决此问题。

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 过程如下:1.创建Kml文件2.添加.zip文件3.根据名称将kml文件添加到zip 4.保存.zip

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. 问题是,当我打开目录时,.zip包含的内容看起来像.kml文件的文件结构,而不仅仅是文件。

For example if the .kml was inside c:/foldera/folderb. 例如,如果.kml位于c:/ foldera / folderb内部。 The zip would contain c:/foldera/folderb/kml. 压缩文件应包含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: 由于某些原因,您有两个addfile:

$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 basename()只返回文件名,去掉路径- http://www.php.net/manual/en/function.basename.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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