Jump to content

Zip File To Memory.


The Little Guy

Recommended Posts

I have this method that puts files into a zip file from an array list, it looks like this:

public function zip($files = array(), $maxMB = 2, $flag = ZIP_NEW){
$this->memory_size = $maxMB * 1024 * 1024;
$zip_id = $this->zip_id;
$this->zip[] = new ZipArchive();
$this->zip_id++;
if($flag == ZIP_NEW){
	$open = $this->zip[$zip_id]->open("php://temp/maxmemory:$this->memory_size", ZipArchive::CREATE);
	$files = (array)$files;
	if(!empty($files)){
		foreach($files as $file){
			if(is_file($file)){
				$this->zip[$zip_id]->addFile($file);
			}
		}
	}
	$this->zip[$zip_id]->close();
}
$this->function_name = __FUNCTION__;
return $this;
}

 

I then download them like this:

public function download($download_data, $download_name, $type = DOWNLOAD_FILE){
header("Pragma: public;");
header("Expires: 0;");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0;");
header("Content-Type: application/force-download;");
header("Content-Type: application/octet-stream;");
header("Content-Type: application/download;");
header("Content-Transfer-Encoding: binary;");
header("Content-Disposition: attachment; filename=$download_name;");
if($type == DOWNLOAD_FILE){
	header("Content-Length: ".filesize($download_data).";");
	if($download_data == PHP_MEMORY){
		$handle = fopen(PHP_MEMORY."/maxmemory:$this->memory_size", 'r+');
		echo stream_get_contents($handle);
	}else{
		readfile($download_data);
	}
}elseif($type == DOWNLOAD_STRING){
	header("Content-Length: ".mb_strlen($download_data).";");
	echo $download_data;
}
$this->function_name = __FUNCTION__;
return $this;
}

 

When I run this code:

$files = array("file1.php","file2.php");
$live->zip($files)->download(PHP_MEMORY, "download.zip");

 

It downloads the file, the only problem is, is that when I open the file I get an error saying the file can not be opened. I know when I actually save it to a file instead of in memory it works. So, I am not sure if it isn't working in the download portion or the create file portion, as I have never worked with php://temp before. Any thoughts on why this doesn't work when using php://temp?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.