문제

I want to create 5 different files that store data from my database. I want to zip up the 5 files and have this function return the zip.

Can I create the 5 files without actually writing them to disk? The data I get from the db are only strings, so each file will be a long string.

I simply want to do this:

function getZippedFiles()
 // Create 1..5 files
 // Zip them up
 // Return zip
end

main()
// $zip_file = getZippedFiles();
end

Any information on how to do this is much appreciated, thanks!

도움이 되었습니까?

해결책

Sure you can, it's pretty simple with ZipArchive

// What the array structure should look like [filename => file contents].
$files = array('one.txt' => 'contents of one.txt', ...);

// Instantiate a new zip archive.
$zip_file = new ZipArchive;

// Create a new zip. This method returns false if the creation fails.
if(!$zip_file->open('directory/to/save.zip', ZipArchive::CREATE)) {
    die('Error creating zip!');
}

// Iterate through all of our files and add them to our zip stream.
foreach($files as $file => $contents) {
    $zip_file->addFromString($file, $contents);
}

// Close our stream.
$zip_file->close();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top