KidsLovePC Contact Us
  Home   PHP  Graphics ASP  JavaScript   J2EE  HTML  JAVA   Swing   Webtip  Forums  Lyric  Free sheet music  Ringtones

Retrieve binary file from MySQL database

Before you start this tutorial, please read the following lessons:

  1. Insert binary file using PHP code
  2. using phpMyAdmin insert binary file
  3. Using form insert binary file into MySQL

This lesson will show you how to retrieve binary files you saved in the above lessons.

Retrieve binary file

Assume you saved a PDF file to the database. It takes 2 step to get saved PDF file from database.

First step, you create a HTML file, like the following:

<html>
<body>
<a href="download.php">My PDF </a>
</body>
</html>

The above code displays a link in the browser. Once clients click on the link, download.php will be called and fetch PDF file from MySQL.

Second step, write download.php file. Here is the PHP code for downloading PDF file from MySQL:

<?php
//connect to database
$username="yourusername";
$password="yourpassword"; //input your password here.
$database="yourdatabase";
//connect to database
$link=mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("<b>Unable to specified database</b>");

//query database
$query = "SELECT image FROM youtablename WHERE id = 10";

$result = mysql_query($query) or die('Error, query failed');
$content=mysql_result($result,0,"image");

//send pdf to requesting page
header("Content-type: application/pdf");
echo $content;

mysql_close($link); //close database connection
?>

In the above example, we assume the PDF file is saved in table named youtablename with a id =10.

Please note that header("Content-type: application/pdf"); must be put before echo $content; Otherwise, it won't work properly. header() function sends a HTTP header to browser, which tells the browser what kind of file it is sending.

If you retrieve a .jpg file, you need use:

header("Content-type: image/jpg");

If you retrieve a .gif file, you need use:

header("Content-type: image/gif");

If you retrieve a .mid file, you need use:

header("Content-type: audio/mid");

....

Use GET method in downloading code

The above code is very easily changed to GET style. So download.php could be used for download any files without changing a bit.

First change your HTML code to:

<html>
<body>
<a href="download.php?id=10">My PDF </a>
</body>
</html>

Second change your download.php to the following

<?php
//connect to database

$id=$_GET['id'];//get id
$username="yourusername";
$password="yourpassword"; //input your password here.
$database="yourdatabase";
//connect to database
$link=mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("<b>Unable to specified database</b>");

//query database
$query = "SELECT image FROM youtablename WHERE id = $id";

$result = mysql_query($query) or die('Error, query failed');
$content=mysql_result($result,0,"image");

//send pdf to requesting page
header("Content-type: application/pdf");
echo $content;

mysql_close($link); //close database connection
?>

download.php will take id parameters from requesting page and becomes more flexible.

 

 
PHP Calendar
Comprehensive PHP Web Calendar
Configurable PHP Calendar
PHP Calendar
PHP Image Processing
PHP grahic softwares
PHP create real-time dynamic chart
create antispam image(image verification)
Add water mark to image
PHP Image processing introduction
Add text on Image
Create thumbnail for image
Create image from text on fly
PHP code sample
PHP create dynamic PDF with FPDF
PHP create dynamic PDF with PDFLib
PHP create dynamic PDF with ClibPDF
Improved PHP configurable hit counter
Install phpMyAdmin,MySql, PHP
PhpMyAdmin export/import data
A simple PHP RSS feed reader
PHP include (SSI)
Working with Forms
PHP formmail Generator
Using phpMyAdmin Create Database
Insert Form Data into Database
Retrieve data from Database
Retrieve multiple records
Batch insert text using phpMyAdmin
Using phpMyAdmin insert binary file
Insert binary file using PHP code
Using form insert binary file into MySQL
Retrieve binary file from MySQL
Image hosting business in minutes
Send HTML mail in PHP
Send bulk emails in PHP
Polling/Rating System in PHP
Polling/Rating System in PHP(radio button)
Check visitor's IP address
Check reciprocal link
PHP tutorial
What is PHP?
Install PHP on Windows XP (part1)
Install PHP on Windows XP (part2)
PHP open and closing tags
PHP types
Variables
Constants
Operators(part1)
Operators(part2)
Logic Structures
Include files
Loops
Set cookie and delete cookie
Most often used MySQL functions

 

 

Printer Inkjet A Z lyricsLearn ChineseHome|PHP|Swing|HTML|JavaScript|ASP|JAVA|Forum|WebTip|Contact Us
Copyright © kidslovepc.com All rights reserved(new). Privacy|Copyright
designed by info@kidslovepc.com

Please help us cover the web site maintenance fee.
barcode scanner Thermal Printer credit card machine bar code printer