Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php creating zips without path to files inside the zip

Tags:

directory

php

zip

I'm trying to use php to create a zip file (which it does - taken from this page - http://davidwalsh.name/create-zip-php), however inside the zip file are all of the folder names to the file itself.

Is it possible to just have the file inside the zip minus all the folders?

Here's my code:

function create_zip($files = array(), $destination = '', $overwrite = true) {      if(file_exists($destination) && !$overwrite) { return false; };     $valid_files = array();     if(is_array($files)) {         foreach($files as $file) {              if(file_exists($file)) {                  $valid_files[] = $file;             };         };     };     if(count($valid_files)) {          $zip = new ZipArchive();         if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {              return false;         };         foreach($valid_files as $file) {              $zip->addFile($file,$file);         };         $zip->close();         return file_exists($destination);     } else {         return false;     };  };   $files_to_zip = array('/media/138/file_01.jpg','/media/138/file_01.jpg','/media/138/file_01.jpg');  $result = create_zip($files_to_zip,'/...full_site_path.../downloads/138/138_files.zip'); 
like image 568
SoulieBaby Avatar asked Oct 22 '10 00:10

SoulieBaby


People also ask

How do I zip a folder and contents?

Press and hold (or right-click) the file or folder, select (or point to) Send to, and then select Compressed (zipped) folder. A new zipped folder with the same name is created in the same location.

How do I exclude a zip file?

Exclude Multiple Files/Directories from Zip Archive You can define -x multiple times in a single zip command to exclude multiple files and directories from zip archive.

How do I add files to a zip file?

Select the files you want to add to the zip file. Right-click one of the files. A menu will appear. In the menu, click Send to and select Compressed (zipped) folder.

How do I zip a file without path?

If you are doing this in a script and don't want to have to hardcode the parent directory, you can do this: pushd /Users/me/development/something/folder/; zip -r ../out. zip ../$(basename $PWD) popd; The ../$(basename $PWD) will ensure that the files are retained in the parent directory when extracted.


2 Answers

The problem here is that $zip->addFile is being passed the same two parameters.

According to the documentation:

bool ZipArchive::addFile ( string $filename [, string $localname ] )

filename
The path to the file to add.

localname
local name inside ZIP archive.

This means that the first parameter is the path to the actual file in the filesystem and the second is the path & filename that the file will have in the archive.

When you supply the second parameter, you'll want to strip the path from it when adding it to the zip archive. For example, on Unix-based systems this would look like:

$new_filename = substr($file,strrpos($file,'/') + 1); $zip->addFile($file,$new_filename); 
like image 58
Nathan Osman Avatar answered Oct 31 '22 13:10

Nathan Osman


I think a better option would be:

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

Which simply extracts the filename from the path.

like image 22
Carlos Évora Avatar answered Oct 31 '22 13:10

Carlos Évora