WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Functions for loading images into a MySQL database and displaying them.
Categories : Graphics, HTML and PHP, MySQL, PHP, Databases Update Picture
Patrick Ryans
Date : Jul 14th 1998
Grade : 3 of 5 (graded 19 times)
Viewed : 58136
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Patrick Ryans
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

/* Image Database Functions

by: J. Patrick Ryans jpatrick@hellyeah.com July 1998

The following are provided niether with copyright or warranty. I do not
believe that any of the code in this file is copyrighted by anyone else, but
if it is, please let me know.

I gathered these functions from a larger php3 project to manage a
large and dynamic web site by keeping all html, images, and other file
contained in a MySQL database. I couldn't find one single example that
explained how to insert binary images into a database and then extract them
and display it to the browser.

The two major functions needed to load and display images are shown below,
but I have left out many details such as how to connect to the database,
how to use html forms, etc. That information can be picked up from a number
of places. I.e. check out:

http://webdev.berber.co.il
for examples of how to connect to a database, and

http://liquid-sky.media.mit.edu/file_upload.html
for how to use forms to upload files

My database has a table for storing binary data such as images and other
types. The schema is as follows:

#
# Table structure for table 'binary'
#
CREATE TABLE binary (
id int(11) DEFAULT '0' NOT NULL auto_increment,
title varchar(200) DEFAULT '' NOT NULL,
auth_id varchar(16) DEFAULT '' NOT NULL,
description varchar(200),
category varchar(50) DEFAULT '' NOT NULL,
body longblob,
cr_date date,
datestamp timestamp(14),
type varchar(50) DEFAULT '' NOT NULL,
KEY category (category),
KEY title (title),
KEY auth_id (auth_id),
PRIMARY KEY (id)
);
*/


/* Loading binary files into the database

There isn't room here to show the html forms, how to connect to the
database and the code for uploading the file into the database, so I'm
going to have to be a little terse here. I'm going to assume that you
know how to use forms to upload files, and I'm going to assume that you
know how to connect to your database which has a table of structure
similar to the schema shown above.

This script is expecting to recieve the following variables from the form:

$title // the title of the file
$auth_id // the id name of the author - field is specific to my needs
$desc // a short description
$cat // category, for organization purposes.
$type // mime type - something like "image/gif"

Be forewarned, error checking is very minimal in this routine, you will
probably want to do some more sanity checking in a production environment.
In my forms, I query the database to present a select list of valid author
ids and mime types.
*/
<? // load.php3

include "admin_connect.ini"; // connect as a user with insert auth.
include "main_h.inc"; // an html header for the page
echo "<center>\n";


$date = date("Y-m-d");
//$title = htmlentities($title); // you may want to clean up the input
// with statements like this.

// Check to see if a file was included in the input="file" tag

if(chop($fileinput)!=""){

// $fileinput should point to a temp file on the server
// which contains the uploaded image. so we will prepare
// the file for upload with addslashes and form an sql
// statement to do the load into the database.

$image = addslashes(fread(fopen($fileinput,"r"), 1000000));
$SQL = "Insert Into $PhotoTable
(title,auth_id,description,category,body,cr_date,type) values ('$title',
'$auth_id', '$desc', '$cat', '$image','$date','$type')";

// now we can delete the temp file
unlink($fileinput);
}
else{
echo "no file entered on form";
exit;
}

$Result = mysql_db_query ( $DB, $SQL );

if($Result==0){
echo "unsuccessful add";
}
else{
echo "successful add";
}
include "main_f.inc";
?>

/* Displaying an image from the database.

This is a very simple way to display the image that was loaded in the above
script. In this case we will query the image by title, but it would be
a simple matter to add other queries.

You must have the mime type for the image stored in the record with the
image data. As per the schema above, the mime type is stored in the 'type'
field.

Be careful that any includes and initialization files do not send anything
to the browser. You want the Header("Content-type: $type"); to be the first
line served.

To use the following script to display an image, use an image tag similar
to the following:

<img src="image.php3?title=myimage.gif">

*/
<? // image.php3

// connect to database with select access
include "user_connect.ini";
$SQL = "select body,type from $PhotoTable where title='$title'";
$Show = mysql ( $hDB, $hSQL );
$Rows = mysql_num_rows($hShow);

if($Rows<1){
// no image matches this query
}
else{
// at least one image has this title
$getPhoto = mysql_fetch_object($Show);

// we need to determine the mime type
$Type = $getPhoto->type;

// and send the correct header to the browser
Header("Content-type: $Type");

// now send the image
$Body = $getPhoto->body;
echo $Body;
flush();
}
?>




Creating thumbnails from MySQL Blobs online
Categories : PHP, MySQL, Graphics, HTML and PHP, Databases
html split bar used to split in multiple pages a database result
Categories : HTML and PHP, Databases, MySQL, PHP
Automatically printing the contents of an sql table in MySQL.
Categories : MySQL, PHP, HTML and PHP, Databases
Message of the Day - Random Message (Needs MySQL!)
Categories : Databases, HTML and PHP, PHP, MySQL
Alternating background color for HTML table rows
Categories : PHP, Databases, MySQL, HTML and PHP
A very simple way to build and do a hierarchical html categories browser without javascript , just using html php and mySql
Categories : HTML and PHP, Databases, Algorithms, PHP, MySQL
Pull Down Surfing - Surf on Change
Categories : Java Script, MySQL, HTML and PHP, PHP, Databases
Dynamically generated pop-ups (Select items)
Categories : PHP, HTML and PHP, MySQL, Databases
How can i Preload a 'SELECT MULTIPLE'?
Categories : HTML and PHP, PHP, MySQL, Databases
Record Set Paging with PHP (RSP)
Categories : PHP, MySQL, Navigation, Databases, HTML and PHP
dynamic table columns
Categories : PHP, HTML and PHP, Arrays, Databases, MySQL
Editing the virtusertable and sendmail.cw via PHP3.0 and Mysql
Categories : MySQL, HTML and PHP, PHP, Databases
This function will populate the options in a drop down HTML select list in a form from a database query.
Categories : MySQL, General SQL, PHP, HTML and PHP, Databases
Paginating the mySQL data
Categories : PHP, Algorithms, Databases, MySQL, HTML and PHP
The simple counter with use MySql and gd.
Categories : MySQL, HTTP, Graphics, PHP, Databases
 Colby Rice wrote : 550
While this is GREAT really i love this example.. one might note that the table structure should read:

CREATE TABLE cool (

or something else other then a reserved word :&gt; like binary.. 
 
 abhishek jangid wrote : 710
i am use this script but error come like  no file select for upload why this error come please any one know about tis please inform to me how i am remove this error
 
 David Perez wrote :793
A got this sample runing into a linux box, but i was trying to use it into a w2k with mysql for win and apache for win, lust like i can not upload images &gt; 0.8k
into the mysql.ini i have done the maximun packed size=10M but still have the problem, some sugestion?