ZipArchives stores absolute paths

Can I draw files using relative paths?

For instance:

$zip->addFile('c:/wamp/www/foo/file.txt');

ZIP must have a directory structure, for example:

 foo -> file.txt 

and not:

 wamp -> www -> foo -> file.txt 

as default ...

ps: my full code is here (I use ZipArchive to compress the contents of the directory into a zip file)

+4
source share
3 answers

See the definition of the addFile() function, you can override the archive file name:

 $zip->addFile('/path/to/index.txt', 'newname.txt'); 
+9
source

If you are trying to recursively add all subfolders and files to a folder, you can try the code below (I changed this code / note in the php manual ).

 class Zipper extends ZipArchive { public function addDir($path, $parent_dir = '') { if($parent_dir != ''){ $this->addEmptyDir($parent_dir); $parent_dir .= '/'; print '<br>adding dir ' . $parent_dir . '<br>'; } $nodes = glob($path . '/*'); foreach ($nodes as $node) { if (is_dir($node)) { $this->addDir($node, $parent_dir.basename($node)); } else if (is_file($node)) { $this->addFile($node, $parent_dir.basename($node)); print 'adding file '.$parent_dir.basename($node) . '<br>'; } } } } // class Zipper 

So basically, this means that it does not include directories (absolute path) to the actual directory / folder you want to archive, and instead only runs from the actual folder (relative path) archived.

+3
source

Here is a modified version of the Paolo script to also include point files such as .htaccess, and it should also be a little faster since I replaced glob by opendir as shown here .

 <?php $password = 'set_a_password'; // password to avoid listing your files to anybody if (strcmp(md5($_GET['password']), md5($password))) die(); // Make sure the script can handle large folders/files ini_set('max_execution_time', 600); ini_set('memory_limit','1024M'); //path to directory to scan if (!empty($_GET['path'])) { $fullpath = realpath($_GET['path']); // append path if set in GET } else { // else by default, current directory $fullpath = realpath(dirname(__FILE__)); // current directory where the script resides } $directory = basename($fullpath); // parent directry name (not fullpath) $zipfilepath = $fullpath.'/'.$directory.'_'.date('Ym-d_His').'.zip'; $zip = new Zipper(); if ($zip->open($zipfilepath, ZipArchive::CREATE)!==TRUE) { exit("cannot open/create zip <$zipfilepath>\n"); } $past = time(); $zip->addDir($fullpath); $zip->close(); print("<br /><hr />All done! Zipfile saved into ".$zipfilepath); print('<br />Done in '.(time() - $past).' seconds.'); class Zipper extends ZipArchive { // Thank to Paolo for this great snippet: http://stackoverflow.com/a/17440780/1121352 // Modified by LRQ3000 public function addDir($path, $parent_dir = '') { if($parent_dir != '' and $parent_dir != '.' and $parent_dir != './') { $this->addEmptyDir($parent_dir); $parent_dir .= '/'; print '<br />--> ' . $parent_dir . '<br />'; } $dir = opendir($path); if (empty($dir)) return; // skip if no files in folder while(($node = readdir($dir)) !== false) { if ( $node == '.' or $node == '..' ) continue; // avoid these special directories, but not .htaccess (except with GLOB which anyway do not show dot files) $nodepath = $parent_dir.basename($node); // with opendir if (is_dir($nodepath)) { $this->addDir($nodepath, $parent_dir.basename($node)); } elseif (is_file($nodepath)) { $this->addFile($nodepath, $parent_dir.basename($node)); print $parent_dir.basename($node).'<br />'; } } } } // class Zipper ?> 

This is a stand-alone script, just copy it to a .php file (for example: zipall.php) and open it in your browser (for example: zipall.php?password=set_a_password , if you do not set the correct password, the page will remain blank for security) . You must use an FTP account to receive the zip file afterwards, this is also a security measure.

+1
source

Source: https://habr.com/ru/post/1439888/


All Articles