Access Keys:
Skip to content (Access Key - 0)
Home (Access Key - 1)
All spaces... (Access Key - 3)
Log in (Access Key - 5)
Sign up (Access Key - 6)
Development

Mambo Manual is part of the documentation project for the Mambo open source content management system

Toggle Sidebar

CSV Export Class - Zip File


We have our csv text ready to export. The first thing we'll do is create a temporary file we can use when we create the zip file. I'm going to do something a little different here. I've had some problems trying to push files that contain the directory path. To overcome that, we'll just change directories using chdir(). As we work with the files, we'll leave out the path.

When we call the function we'll pass it both the sql statement and a filename without the extension. Normally, I just use the table name. But if we're working with multiple tables, we need to come up with something else.

Let's save our temporary file setting up some paths and common variables as we go:

// in case we send the name with a file extension
$name = explode('.', $name);
$name = $name[0];

$tempFile = $name.'.csv';
$zipFile = $name.'.zip';
$returnPath = myUtil::fixPath(myCompat::adminPath());
$tempPath = myUtil::fixPath(myCompat::adminBasePath().'/includes/temp');

chdir($tempPath);
$fp = fopen($tempFile, 'w');
fwrite($fp, $text);
fclose($fp);

Now we'll create the zip file using pclZip:

require_once myUtil::fixPath(myCompat::adminBasePath().'/includes/pcl/pclzip.lib.php');
$archive = new PclZip($zipFile);
$results = $archive->add($tempFile, PCLZIP_OPT_REMOVE_PATH, $tempPath);
if ($results == 0){
	die("Error : ".$archive->errorInfo(true));
}
if (file_exists($tempFile)) unlink($tempFile);

Pushing the content

We're almost done. All we need to do now is push the zip file to the browser. We'll do that in the next section.

Click the Next Page link below to continue your journey.

Next Page: CSV Export Class - Pushing the content

Toggle Sidebar
Space Navigation
Added by Lynne Pope on 30 Dec, 2007 02:50, last edited by Lynne Pope on 30 Dec, 2007 02:50

Adaptavist Theme Builder Powered by Atlassian Confluence
Free theme builder license