John Sk
John Sk

Reputation: 117

Is it possible to create a CSV file and make ZIp download in phalcon

I am searching for the CSV file creation in Phalcon .I got some CSV documentation in phalcon but it was not giving clear idea.Can some one post the code for it.Thanks in Advance.

namespace Business\Controllers\API;
use Phalcon\Mvc\Controller;
use Phalcon\Http\Response;

class DownloadController extends \Business\Controllers\API\ApiControllerBase

{

public function createZipAction()
{   

        // create your zip file
        $zipname = 'bvcards.zip';
        $zip = new \ZipArchive;
        $zip->open($zipname, ZipArchive::CREATE);

      echo "here i am";
      exit;
 }

}

The above code not executing because Zip Archive making some issue.

Upvotes: 2

Views: 742

Answers (3)

Aman Garg
Aman Garg

Reputation: 3290

The same like in PHP. Please check fgetcsv function: http://php.net//manual/en/function.fgetcsv.php

Upvotes: 1

John Sk
John Sk

Reputation: 117

The below code worked for me .

namespace Business\Controllers\API;
use Phalcon\Mvc\Controller;
use Phalcon\Http\Response;
use ZipArchive;

class DownloadController extends \Business\Controllers\API\ApiControllerBase

{

public function createZipAction()
{   

    // create your zip file
    $zipname = 'bvcards.zip';
    $zip = new \ZipArchive;
    $zip->open($zipname, ZipArchive::CREATE);

  echo "here i am";
  exit;

}

}

Upvotes: 0

Nikolay Mihaylov
Nikolay Mihaylov

Reputation: 3876

Here is a full example:

    $zip = new \ZipArchive();
    $filename = 'test.zip';

    if ($zip->open($filename, \ZipArchive::CREATE)!==TRUE) {
        exit("cannot open <$filename>\n");
    }        
    $zip->addFile('robots.txt', '.htaccess'); 
    $zip->close();

Note the \ before ZipArchive in the open() method. You are missing it and you were probably receiving namespace error.

Please note: in the current example used and generated files are relative to the index.php since in my example this is my bootstrap file. You should set paths according to your configuration.

Upvotes: 0

Related Questions