简体   繁体   English

PHP-从一个文件创建一个zip文件

[英]PHP - Created a zip file from a single file

have everything working including all the password protection parts but can't get past this last issue. 一切正常,包括所有密码保护部分,但无法解决上一期问题。

The requirement is on a Laravel app, a CSV is created and downloaded as a password protected zip. 要求是在Laravel应用上,创建CSV并将其下载为受密码保护的zip。

To do this I have a class that stores the CSV locally, then a method that calls the following: 为此,我有一个在本地存储CSV的类,然后是一个调用以下内容的方法:

echo system('zip -P ' .$this->password. ' ' .$this->getZipStoragePath(). ' ' .storage_path('app/').$this->getFilePath());

This 'works' in that it creates a password protected zip file. 这个“有效”的方法是创建一个受密码保护的zip文件。 But when extracted, there is a folder structure of my machine up to the point to point to find this file. 但是,当解压缩时,我的机器存在一个文件夹结构,直到找到该文件为止。

So, extracting this created zip gives me a directory of: 因此,解压缩此创建的zip会给我一个目录:

Users > Craig > Apps > Project Name > Storage > app > temp > then the file here.

Is there a way I can use the same zip system method but have it only zip up the file and not the whole path as returned with the storage_path() method? 有没有一种方法可以使用相同的zip系统方法,但只能压缩文件而不是使用storage_path()方法返回的整个路径?

It seems that you want the -j flag: 似乎您需要-j标志:

-j
--junk-paths
     Store just the name of a saved file (junk the path), and do
     not store directory names. By default, zip will store the
     full path (relative to the current directory).

Also note that you can use the native PHP Zip support, and call addFile() with the optional second argument to override the filename inside the archive. 还要注意,您可以使用本机PHP Zip支持,并使用可选的第二个参数调用addFile()来覆盖存档中的文件名。

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

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