|
|
|
/* 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 | | | Dynamically generated pop-ups (Select items) Categories : PHP, HTML and PHP, MySQL, Databases | | | Message of the Day - Random Message (Needs MySQL!) Categories : Databases, HTML and PHP, PHP, MySQL | | | Required form fields that pull from MySQL database Categories : PHP, HTML and PHP, Databases, MySQL | | | mySQL/PHP/search with multientry
form and table output with colored rows Categories : PHP, Beginner Guides, MySQL, HTML and PHP, Databases | | | Storing / Retrieving pictures from database This can be used for banner / ads exchange. very useful for people developing big portal with ads in the site........ Categories : PHP, MySQL, Databases, HTML and PHP | | | Record Set Paging with PHP (RSP) Categories : PHP, MySQL, Navigation, Databases, HTML and PHP | | | Alternating background color for HTML table rows Categories : PHP, Databases, MySQL, HTML and PHP | | | Database resultset navigation Categories : PHP, HTML and PHP, Databases, MySQL, Navigation | | | Function to do live population of HTML's <Select> tag from a Table Categories : PHP, MySQL, HTML and PHP, Databases | | | 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 | | | Automatically printing the contents of an sql table in MySQL. Categories : MySQL, PHP, HTML and PHP, Databases | | | html split bar used to split in multiple pages a database result Categories : HTML and PHP, Databases, MySQL, PHP | | | How can i Preload a 'SELECT MULTIPLE'? Categories : HTML and PHP, PHP, MySQL, 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 :> 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 > 0.8k
into the mysql.ini i have done the maximun packed size=10M but still have the problem, some sugestion?
| |
|
|