Lovelock
Lovelock

Reputation: 8085

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?

Upvotes: 0

Views: 125

Answers (1)

Alex Howansky
Alex Howansky

Reputation: 53563

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.

Upvotes: 1

Related Questions