Posts Tagged ‘PHP’

Creating a Downloadable Spreadsheet from PHP

January 16th, 2009

I have written several systems that show data on a page but also provide a CSV download file either through a textarea with formatted data inside to copy and paste or via a download link and creates a file on the fly.

I recently decided that this wasn’t the best way to do it seeing as the people will want to be using Microsoft Excel to view the data. It is possible to write a spreadsheet using PHP and using an example from AppServ I started to do it this way.

The original link is included above but here is my simplified version with smaller more understandable function naming:

Download the example script here: XLS from PHP Example (723 bytes)

<?php

function xls_BOF() {
	echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
}

function xls_EOF() {
	echo pack("ss", 0x0A, 0x00);
}

function xls_write_cell($row, $col, $value='') {
	if (is_numeric($value)) {
		echo pack("sssss", 0x203, 14, $row, $col, 0x0);
		$value = pack("d", $value);
	} else {
		$l = strlen($value);
		echo pack("ssssss", 0x204, 8 + $l, $row, $col, 0x0, $l);
	}

	echo $value;
}

// Send Headers to set the Filename and force a download of the contents of the script
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");

 // Put the filename in \"'s if you want it to include spaces
header("Content-Disposition: attachment;filename=" . $filename . ".xls");
header("Content-Transfer-Encoding: binary ");

xlsBOF(); //Start the Spreadsheet

xls_write_cell(0, 1, 'Column 1');
xls_write_cell(0, 2, 'Column 2');
xls_write_cell(0, 3, 'Column 3');

$row = 1;
for ($i=0; $i<2; $i++) {
	xls_write_cell($row, 0, 'Row ' . $row . ' Column ' . $i);
	xls_write_cell($row, 1, 'Row ' . $row . ' Column ' . $i);
	xls_write_cell($row, 2, 'Row ' . $row . ' Column ' . $i);

	$row++;
}

xlsEOF(); //End the Spreadsheet

exit(); //Exit the script after download

?>

IE Annoyance when downloading PDFs

September 24th, 2008

I came across this error at work and it took me a good three hours to fix! The problem was when a user tried to download a pdf file from one of our sites in internet explorer. It worked fine in Firefox though! The error message was:

The file could not be written to the cache

Luckily a far low down on Google had a sensible answer which was to add the following code (PHP) before the call to session_start();

if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
    session_cache_limiter("public");
}

Other sites were talking about security settings in versions of IE below 6 but they didn’t really help. I thought I would post this in the hope that it helps anyone else that has the same problem.

Zen Cart Welcome Email Editor

July 31st, 2008

I’ve just dug up a script that I wrote a while back for someone. It’s not the most advanced thing in the world and it does what it says on the tin.

Within Zen Cart there is a menu on the admin pages called ‘Tools’ which has an option called ‘Email Welcome’. This script is a default Zen Cart page to simply show you what the welcome email looks like.

Screenshot 1

hint: be sure to turn on html emails and it will show you both the html and text versions (default text only is set)

I have modified this script to allow the editing of these pages if you have little or no knowledge of writing PHP although some HTML skill is required.

Zen Cart puts together a number of PHP Defines to build the welcome email and normally when you want to modify it then you have to trawl through three of four files of these defines to get to the one you want.

This script basically does that for you and parses/updates the files necessary with your new information. The following image shows the new page and i’m sure no explanation is needed to show you how to use it.

Screenshot Thumbnail 2

Known Bugs: the only thing I know to be wrong with it is that some defnes reference other defines and this script doesn’t respect that. There are only one or two defines like this so nothing to worry about unless you intend to change the name of the shop owner frequently. If you do change it, however, then just make sure you use this system to update the welcome email at the same time. Nice and easy!

UPDATE: Please make sure to chmod your languages/[language]/email_extras.php, languages/[language]/create_account.php and /languages/[language].php files (in both catalogue and admin) to be writable by apache if not this may not work.