Directory structure for downloading without saving to the server

I have a project in which I would like to create a dynamic directory tree structure without storing it on the server, compress and push it to load. Can this be done?

+2
source share
2 answers

Perhaps you are looking for something like ZipArchive::addEmptyDir , which you can use to create an empty directory in a ZIP archive. You can add files to this directory later.

You still need to save the resulting archive to disk ...

 $file = tempnam("tmp", "zip"); $zip = new ZipArchive(); $zip->open($file, ZIPARCHIVE::CREATE); $zip->addEmptyDir('newDirectory'); $zip->addFromString('newDirectory/demo.txt', 'file contents'); $zip->close(); // Stream the file to the client header('Content-Type: application/zip'); header('Content-Length: ' . filesize($file)); header('Content-Disposition: attachment; filename="some_archive_file.zip"'); readfile($file); unlink($file); 

If you want to transfer the archive without saving the archive at your end, you can try to look at the PHPZip class

+2
source

You can use passthru with a combination of command line archiving utilities, passthru .:

 passthru("tar dir0/ dir1/ dir2/dir23/dir234/ dir4/file4.txt | gzip -f"); 
0
source

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


All Articles