PHP tip: How to make a file downloadable through your script

Tags:

This seems to be a relatively common question on #zftalk nowadays, so here’s a quick wrapup! The traditional plain-PHP way $file = file_get_contents(’some.zip’); header(’Content-Type: application/zip’); header(’Content-Disposition: attachment; filename="some.zip"’); header(’Content-Length: ‘ . strlen($file)); echo $file;$file = file_get_contents(‘some.zip’); header(‘Content-Type: application/zip’); header(‘Content-Disposition: attachment; filename="some.zip"’); header(‘Content-Length: ‘ . strlen($file)); echo $file; The Zend Framework controller way //this is inside an action in one of …

Making a PDF generator class using Zend_Pdf

Tags:

There are some expensive libraries that you can use to generate PDF files… but why bother with them, when there are good free alternatives like Zend_Pdf? In three steps, you can generate a fancy report: Create a PDF template Insert data to template with Zend_Pdf Profit $$$ Since this is a quite repetitive task, let’s also create a PDF generator …

Generic collections in PHP

Tags:

Today while reading PHP::Impact’s refactoring guideline post I was reminded about collections. As you probably know, arrays are very flexible in PHP and probably a good choice for many data storing tasks. Strictly typed languages usually use “generic” collection classes instead of arrays. They are kind of like PHP arrays which the programmer can tell which type of items to …

Database helper for PHPUnit

Tags:

When testing code which uses the database, you would usually want to make sure the database stays pristine for each test – All tables should be empty, as any extra data could interfere with the tests. You could probably write an extended Testcase class which automatically does this before each test in setUp and afterwards in tearDown, but it may …

PRADO: PHP goes ASP.NET (ish)

Tags:

Lately, I’ve been checking out PRADO, which is “a component-based and event-driven programming framework for developing Web applications in PHP 5”. What that actually means is that you can use components, such as a data grid or a button, and work with them based on events without having to think about the traditional things associated with such: Parsing out POST …

View inheritance and “blocks” in Zend_View

Tags:

Some frameworks like Django and PRADO have the concept of view inheritance, or a “master page” like in ASP.NET. Essentially it’s a similar idea as Zend_Layout is in Zend Framework, but the difference is that you can define the master/parent view inside the view itself. For example, in Django {% extends “base.html” %} would make the current template extend “base.html”, …

Setting up command-line PHP on Windows

Tags:

On *nix systems you usually have the php executable available in shell, so you can run command-line scripts like unit tests and such, but on Windows this isn’t usually the case. Let’s fix that! There’s two ways to do this: Temporary and permanent

Is Smarty really obsolete?

Tags:

There was a short discussion on Zend Framework general mailing list about Smarty being obsolete. I decided to take my opinion here, since the discussion was getting quite off-topic… Arguments against Smarty presented were mainly the facts that it is PHP4 and its no longer necessary in “modern” web development. Note: This post was written in 2008.. Although the points …

Automating creation and caching of image thumbnails

Tags:

The other day I needed to create some thumbnail images. You know, the usual stuff: Admin uploads image, needs it resized to a smaller one for preview or such… I thought, why bother with writing code manually in the admin to create the thumbnail? I mean, the requirements for its size might change, the path to thumbnails might change… The …