title: error displaying images - code from PHP & MySQL Novice to Ninja
body: <p>I am new to PHP and MySQL and in 2 chapters of Kevin Yank's book - PHP &amp; MySQL Novice to Ninja there are mistakes in the code. The only one I haven't figured out lies in chapter 12, and having tried suggestions from multiple posts on this and other fora, nothing works. Thanks to in advance for your help</p>
  
  <p>Problem: Blob gives problem to load:</p>
  
  <blockquote>
    <p>The image "<a href="http://localhost/chapter12/filestore5/index.php?action=view&amp;id=5" rel="nofollow noreferrer">http://localhost/chapter12/filestore5/index.php?action=view&amp;id=5</a>" cannot be displayed because it contains errors</p>
  </blockquote>
  
  <p>All other functions: upload, description, delete works perfectly.</p>
  
  <p>index.php file</p>
  
  <pre><code>&lt;?php
  include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/magicquotes.inc.php';
  
  if (isset($_POST['action']) and $_POST['action'] == 'upload') {
  
      // Bail out if the file isn't really an upload
  
      if (!is_uploaded_file($_FILES['upload']['tmp_name'])) {
          $error = 'There was no file uploaded!';
          include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php';
  
          exit();
      }
  
      $uploadfile = $_FILES['upload']['tmp_name'];
      $uploadname = $_FILES['upload']['name'];
      $uploadtype = $_FILES['upload']['type'];
      $uploaddesc = $_POST['desc'];
      $uploaddata = file_get_contents($uploadfile);
      include 'db.inc.php';
  
      try {
          $sql = 'INSERT INTO filestore SET
      filename = :filename,
      mimetype = :mimetype,
      description = :description,
      filedata = :filedata';
          $s = $pdo-&gt;prepare($sql);
          $s-&gt;bindValue(':filename', $uploadname);
          $s-&gt;bindValue(':mimetype', $uploadtype);
          $s-&gt;bindValue(':description', $uploaddesc);
          $s-&gt;bindValue(':filedata', $uploaddata);
          $s-&gt;execute();
      }
  
      catch(PDOException $e) {
          $error = 'Database error storing file!';
          include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php';
  
          exit();
      }
  
      header('Location: .');
      exit();
  }
  
  if (isset($_GET['action']) and ($_GET['action'] == 'view' or $_GET['action'] == 'download') and isset($_GET['id'])) {
      include 'db.inc.php';
  
      try {
          $sql = 'SELECT filename, mimetype, filedata
      FROM filestore
      WHERE id = :id';
          $s = $pdo-&gt;prepare($sql);
          $s-&gt;bindValue(':id', $_GET['id']);
          $s-&gt;execute();
      }
  
      catch(PDOException $e) {
          $error = 'Database error fetching requested file.';
          include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php';
  
          exit();
      }
  
      $file = $s-&gt;fetch();
      if (!$file) {
          $error = 'File with specified ID not found in the database!';
          include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php';
  
          exit();
      }
  
      $filename = $file['filename'];
      $mimetype = $file['mimetype'];
      $filedata = $file['filedata'];
      $disposition = 'inline';
      if ($_GET['action'] == 'download') {
          $mimetype = 'application/octet-stream';
          $disposition = 'attachment';
      }
  
      // Content-type must come before Content-disposition
  
      header('Content-length: ' . strlen($filedata));
      header("Content-type: $mimetype");
      header("Content-disposition: $disposition; filename=$filename");
      echo $filedata;
      exit();
  }
  
  if (isset($_POST['action']) and $_POST['action'] == 'delete' and isset($_POST['id'])) {
      include 'db.inc.php';
  
      try {
          $sql = 'DELETE FROM filestore
      WHERE id = :id';
          $s = $pdo-&gt;prepare($sql);
          $s-&gt;bindValue(':id', $_POST['id']);
          $s-&gt;execute();
      }
  
      catch(PDOException $e) {
          $error = 'Database error deleting requested file.';
          include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php';
  
          exit();
      }
  
      header('Location: .');
      exit();
  }
  
  include 'db.inc.php';
  
  try {
      $result = $pdo-&gt;query('SELECT id, filename, mimetype, description
      FROM filestore');
  }
  
  catch(PDOException $e) {
      $error = 'Database error fetching stored files.';
      include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php';
  
      exit();
  }
  
  $files = array();
  
  foreach($result as $row) {
      $files[] = array(
          'id' =&gt; $row['id'],
          'filename' =&gt; $row['filename'],
          'mimetype' =&gt; $row['mimetype'],
          'description' =&gt; $row['description']
      );
  }
  
  include 'files.html.php';
  
  ?&gt;
  </code></pre>
  
  <p>HTML file</p>
  
  <pre><code>&lt;?php 
      include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/helpers.inc.php';
  ?&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
      &lt;head&gt;
          &lt;meta charset="utf-8"&gt;
          &lt;title&gt;PHP/MySQL File Repository&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;h1&gt;PHP/MySQL File Repository&lt;/h1&gt;
          &lt;form action="" method="post" enctype="multipart/form-data"&gt;
              &lt;div&gt;
                  &lt;label for="upload"&gt;Upload File:
                  &lt;input type="file" id="upload" name="upload"&gt;&lt;/label&gt;
              &lt;/div&gt;
              &lt;div&gt;
                  &lt;label for="desc"&gt;File Description:
                  &lt;input type="text" id="desc" name="desc"
                      maxlength="255"&gt;&lt;/label&gt;
              &lt;/div&gt;
              &lt;div&gt;
                  &lt;input type="hidden" name="action" value="upload"&gt;
                  &lt;input type="submit" value="Upload"&gt;
              &lt;/div&gt;
          &lt;/form&gt;
          &lt;?php if (count($files) &gt; 0): ?&gt;
          &lt;p&gt;The following files are stored in the database:&lt;/p&gt;
          &lt;table&gt;
              &lt;thead&gt;
                  &lt;tr&gt;
                      &lt;th&gt;Filename&lt;/th&gt;
                      &lt;th&gt;Type&lt;/th&gt;
                      &lt;th&gt;Description&lt;/th&gt;
                  &lt;/tr&gt;
              &lt;/thead&gt;
              &lt;tbody&gt;
                  &lt;?php foreach($files as $f): ?&gt;
                  &lt;tr&gt;
                      &lt;td&gt;
                          &lt;a href="?action=view&amp;amp;id=&lt;?php htmlout($f['id']); ?&gt;
                              "&gt;&lt;?php htmlout($f['filename']); ?&gt;&lt;/a&gt;
                      &lt;/td&gt;
                      &lt;td&gt;&lt;?php htmlout($f['mimetype']); ?&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;?php htmlout($f['description']); ?&gt;&lt;/td&gt;
                      &lt;td&gt;
                          &lt;form action="" method="get"&gt;
                              &lt;div&gt;
                                  &lt;input type="hidden" name="action"
                                      value="download"/&gt;
                                  &lt;input type="hidden" name="id"
                                      value="&lt;?php htmlout($f['id']); ?&gt;"/&gt;
                                  &lt;input type="submit" value="Download"/&gt;
                              &lt;/div&gt;
                          &lt;/form&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;form action="" method="post"&gt;
                              &lt;div&gt;
                                  &lt;input type="hidden" name="action" value="delete"/&gt;
                                  &lt;input type="hidden" name="id"
                                      value="&lt;?php htmlout($f['id']); ?&gt;"/&gt;
                                  &lt;input type="submit" value="Delete"/&gt;
                              &lt;/div&gt;
                          &lt;/form&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;?php endforeach; ?&gt;
              &lt;/tbody&gt;
          &lt;/table&gt;
          &lt;?php endif; ?&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>