Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Mike Wilson www.whisperingwind.co.uk

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

php image from database using PEAR::DB

This fetches an image from a MySQL database (I know, a database may not be the best place to keep images). The image is sent to the web client as an image/jpeg.

<?php
require_once 'DB.php';

define('DB_SERVER', 'localhost');
define('DB_USER', 'php');
define('DB_PASS', '');
define('DB_DATABASE', 'images');
define('DSN', 'mysql://'.DB_USER.':'.DB_PASS.'@'.DB_SERVER.'/'.DB_DATABASE);

global $db;
$db = DB::connect(DSN);

if (PEAR::isError($db)) {
    die($db->getMessage());
}

if (isset($_GET['id'])) {
  header('Content-Type: image/jpeg');
  $id = $_GET['id'];
  $sql = "select image_data from images where id = ?";
  $result =& $db->query($sql, $id);

  if (PEAR::isError($result)) {
    die($result->getMessage());
  } else {
    if ($result->fetchInto($row)) {
      echo $row[0];
    }
  }

} else {
  echo file_get_contents('broken.png');
}
?>
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS