Force a file download

Here’s a small function that will allow you to force a file download.

/**
 * Force a file download via HTTP.
 *
 * File is required to be on the same server and accessible via a path.
 * If the file cannot be found or some other error occurs then a
 * '204 No content' header is sent.
 *
 * @param string $path Path and file name
 * @param string $name Name of file when saved on user's computer,
 *                     null for basename from path
 * @param string $type Content type header info (e.g., 'application/vnd.ms-excel')
 * @return void
 * @access public
 */
/* public static */ function download($path, $name = null, $type = 'binary/octet-stream')
{
    if (headers_sent()) {
        echo 'File download failure: HTTP headers have already been sent and cannot be changed.';
        exit;
    }
    
    $path = realpath($path);
    if ($path === false || !is_file($path) || !is_readable($path)) {
        header('HTTP/1.0 204 No Content');
        exit;
    }

    $name = (empty($name)) ? basename($path) : $name;
    $size = filesize($path);

    header('Expires: Mon, 20 May 1974 23:58:00 GMT');
    header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
    header('Cache-Control: no-store, no-cache, must-revalidate');
    header('Cache-Control: post-check=0, pre-check=0', false);
    header('Cache-Control: private');
    header('Pragma: no-cache');
    header("Content-Transfer-Encoding: binary");
    header("Content-type: {$type}");
    header("Content-length: {$size}");
    header("Content-disposition: attachment; filename=\"{$name}\"");
    readfile($path);
    exit;
}

Very easy to use, too! Here are some examples of how you might call the function:

download('./myfile.txt');

download(__FILE__, 'a file for you.php');

download('/home/you/files/spreadsheet.xml', 'ssheet_' . date('Ymd'), 'application/vnd.ms-excel');
Did you like this? Share it:

Portal (the game from Valve)

A couple days ago I bought Portal via Steam, and even though it’s probably one of the shortest games I’ve played it is undoubtedly one of the best! The premise is pretty simple; solve puzzles by using the portals that you can shoot in to the floor, ceiling or wall and walk through (or in some cases, fall in to or be shot through!) But a simple concept doesn’t mean it’s a simplistic game. It’s great to look at (considering it came from Half-Life 2 this is no surprise), has a fantastic sense of humor running through-out, and will sometimes mess with your head (“…OK, so I have to shoot this wall, to fall through that ceiling and.. woah! I can see me walking through the ceiling from down here!”)

The only downside to this game is that it’s very short – completed within a few hours easily. And that’s only a negative because the game is so captivating that it really leaves you wanting more.

There is a hack to integrate the Portal gun in to Half-Life 2 game play that I am looking forward to trying out because it’ll add a whole new dimension to the game play.

$20 well and truly spent well!

Did you like this? Share it:

Zend Certified Engineer

During the php|works conference in Atlanta this year I took the PHP5 Zend Certification, and I’m pleased to say that today I found out I had passed! So I am now pleased to say that I am a Zend Certified Engineer for both PHP4 and PHP5.

And just to prove it…

Did you like this? Share it:

Simple image view helper for Zend Framework

Here’s a simply view helper for the Zend Framework that can be used to display image tags. It checks to see if the image file exists and if not then it’ll use the data url scheme to output a very simple image that, ironically, says ‘NO IMG’ on it. 🙂 Please note, though, that I’ve only seen Firefox support this scheme, as wonderful as it is!

Continue reading “Simple image view helper for Zend Framework”

Did you like this? Share it: