dee cheok
dee cheok

Reputation: 257

decode base64 and pass to zip file

public function store($location){   
if($this->zip->open($location, file_exists($location) ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE)){
foreach($this->files as $file){
$this->count++;
$this->image_name ="OrderImg".$this->count.".png";
$this->set = str_replace('data:image/png;base64,', '', $file);
$this->set = str_replace(' ', '+', $file);
$this->zip->addFile($this->image_name, base64_decode($file));
}
$this->zip->close();
}
}

how to i decode my canvas data with base64 and put in to my zip file , i cant make it work here , my intention is to get my canvas data decode with base64 and zip it together.

the zip file didt create and i have no idea why.

Upvotes: 0

Views: 2316

Answers (2)

RohitS
RohitS

Reputation: 1046

I might be late for the party but something like this You can try :

public function store($location)
{   
    if($this->zip->open($location, file_exists($location) ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE))
    {
      foreach($this->files as $file) // assuming you have file **array of base64 encoded images** here
      {
        $this->count++;
        $this->image_name ="OrderImg".$this->count.".png";
        $this->set = str_replace('data:image/png;base64,', '', $file);
        $this->set = str_replace(' ', '+', $file);
        $this->zip->addFromString($this->image_name, base64_decode($this->set));// assuming you have base64 encoded string here obtained for $file
      }
        $this->zip->close();
  }
}

as you are adding trying to use base64 encoded string you have to use addFromString() rather the addFile()

as for addfile :

bool ZipArchive::addFile ( string $filename [, string $localname = NULL [, int $start = 0 [, int $length = 0 ]]] )

or simply

bool ZipArchive::addFile ( string $file_name_till_fullpath, string $name_you_want_inarchive )

and for addFromString :

bool ZipArchive::addFromString ( string $localname , string $contents )

Upvotes: 0

Adhan Timothy Younes
Adhan Timothy Younes

Reputation: 570

Try this:

public function store($location){
if($this->zip->open($location, file_exists($location) ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE)){
foreach($this->files as $file){
$this->count++;
$this->image_name ="OrderImg".$this->count.".png";
$file_encoded = base64_encode($file);
$this->zip->addFromString($this->image_name, base64_decode($file_encoded));
}
$this->zip->close();
}
}

Even if is not decoded you can also add it to zip as image file like this:

$this->zip->addFromString($this->image_name,$file_encoded);

Upvotes: 2

Related Questions