Skip to content

Instantly share code, notes, and snippets.

@haric
Created May 20, 2012 18:36
Show Gist options
  • Select an option

  • Save haric/2759071 to your computer and use it in GitHub Desktop.

Select an option

Save haric/2759071 to your computer and use it in GitHub Desktop.
Download file from DB
<?php
ob_start();
ini_set ("display_errors", 1);
if (isset($_REQUEST["id"])) {
$id = $_REQUEST["id"];
$c = mysqli_connect("localhost" , "root", "", "mcdb");
$sql = "SELECT name, type, size, content " ."FROM photo WHERE id = '$id'";
$rc = mysqli_query($c, $sql) or die('Error, query failed');
list($name, $type, $size, $content) = mysqli_fetch_array($rc);
// since we are expecting a single row
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
echo $content;
mysqli_free_result($rc);
mysqli_close($c);
}
ob_end_flush();
ini_set ("display_errors", 0);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment