Search
 
SCRIPT & CODE EXAMPLE
 

PHP

download image from mysql using php

//You can save this to test.php and call it with test.php?id=1 for example
<?php

//Database class with PDO (MySQL/MariaDB)
require_once("database.php"); //If you need this, write to office@predl.cc i'll send you the db class

//Connect to database
$database = new Database();
$pdo = $database->dbConnection();

//Get ID from GET (better POST but for easy debug...)
if (isset($_GET["id"])) {
	$id=(int)$_GET["id"];
} else {
  echo "Wrong input";
  exit;
}

//Prepare PDO SQL
$q = $pdo->prepare("SELECT * FROM `table_with_image` WHERE `id`=:p_id");

//Add some data
$q->bindparam(":p_id", $id); //Filter user input, no sanitize necessary here

//Do the db query
$q->execute();

//If something found (always only 1 record!)
if ($q->rowCount() == 1) {
  
  	//Get the content of the record into $row
	$row = $q->fetch(PDO::FETCH_ASSOC); //Everything with id=$id should be in record buffer
  	
  	//This is the image blob mysql item  
  	$image = $row['image'];
  	
  	//Clean disconnect
  	$database->disconnect();
    
  	//Now start the header, caution: do not output any other header or other data!
  	header("Content-type: image/jpeg");
    header('Content-Disposition: attachment; filename="table_with_image_image'.$id.'.jpg"');
    header("Content-Transfer-Encoding: binary"); 
    header('Expires: 0');
    header('Pragma: no-cache');
    header("Content-Length: ".strlen($image));
    //Output plain image from db
	echo $image;
} else {
  //Nothing found with that id, output some error
  $database->disconnect();
  echo "No image found";
}

//No output and exceution further this point
exit();
Comment

PREVIOUS NEXT
Code Example
Php :: wp wordPress variables de session 
Php :: confirm popup laravel 
Php :: generate a unique id 
Php :: count array index foreach in php 
Php :: php 8 loadmodule 
Php :: pagination using ajax 
Php :: php print 
Php :: php 30days 
Php :: send data with window.location.href 
Php :: php fetch return false 
Php :: php barcode generator 
Php :: php RFC3339 
Php :: php class 
Php :: php thread safe or not thread safe windows 
Php :: how to convert an array to uppercase before storing in database 
Php :: add column migration laravel 8 
Php :: array filter php get first object 
Php :: send gmail 
Php :: id type laravel 
Php :: email verification laravel 
Php :: Remove White Space At Sides 
Php :: wordpress basic auth 
Php :: convert html to pdf php 
Php :: stored procedure laravel 
Php :: how to create object in php 
Php :: testing php 
Php :: Add Custom Field to woocommerce Subscriptions 
Php :: Eloquent orWhere clousure 
Php :: movies -inurl:(htm|html|php|pls|txt) intitle:index.of “last modified” (mp4|wma|aac|avi) 
Php :: if cat 1 then send email woocommerce functions 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =