简体   繁体   中英

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.

To do this I have a class that stores the CSV locally, then a method that calls the following:

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

This 'works' in that it creates a password protected zip file. 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:

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?

It seems that you want the -j flag:

-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.

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