Upload the image to the database using the PHP implementation

Source: Internet
Author: User
Tags exit fread php file mysql database
Upload | data | database If you have any questions please contact me: http://www.webjx.com web@webjx.com
Reprint please indicate the source

Today to teach you how to use the PHP implementation of the image uploaded to the MySQL database. In this tutorial we need to create 3 PHP files:

readdir.php-code to put the picture in the database
Image.php-code to display the actual picture
View.php-code to show how you call a picture in a database

1. Create a database

CREATE TABLE ' images ' (
' Imgid ' INT not NULL auto_increment,
' Sixfourdata ' longtext not NULL,
PRIMARY KEY (' Imgid ')
);
READDIR. Php

Specific content:
?
$DBCNX = mysql_connect ("localhost", "username", "password");
mysql_select_db ("Base64imgdb");
?>
' We need to open a directory
"./"
The ' readdir.php file is located in this directory:
$path = "./";
$dir_handle = Opendir ($path) or Die ("Unable to open Directory $path");

Here are the more difficult parts, we need to study it: the image classification, and read out the use of some data

fopen

' Convert

Base64_encode

' INSERT into the table

?
while ($file = Readdir ($dir_handle)) {
$filetyp = substr ($file,-3);
if ($filetyp = = ' gif ' OR $filetyp = = ' jpg ') {
$handle = fopen ($path. "/" . $file, ' R ');
$file_content = Fread ($handle,filesize ($path. "/" . $file));
Fclose ($handle);
$encoded = Chunk_split (Base64_encode ($file_content));
$sql = "INSERT into images SET sixfourdata= ' $encoded '";
mysql_query ($sql);
}
}
?>

Close the set of directories, and then process:

?
Closedir ($dir_handle);
Echo ("complete");
Mysql_close ($DBCNX);
?>

Read the code for the picture: image.php
This code is more difficult, we have to take a good look at

?
$DBCNX = mysql_connect ("localhost", "username", "password");
mysql_select_db ("Base64imgdb");
?>
We read out the code used by the picture image.php?img=x:
?
$img = $_request["img"];
?>
Then we need to connect to the database and read out
?
$result = mysql_query ("SELECT * from Images WHERE imgid=".) $img. "");
if (!$result) {
Echo ("<b> request Error:".) Mysql_error (). "</b>");
Exit ();
}
while ($row = Mysql_fetch_array ($result)) {
$imgid = $row["Imgid"];
$encodeddata = $row["Sixfourdata"];
}
?>

?
Mysql_close ($DBCNX);
echo Base64_decode ($encodeddata);
?>

Here we want to understand the base64-encoded image Data format.

"Let's take a look at the specific picture!" "VIEW. Php

Image.php?img=1

image.php?img=357



Take a look at the complete example!

readdir.php:
?
###############################
# DB CONNECTION
# Change these VALUES
###############################
$DBCNX = mysql_connect ("localhost", "username", "password");
mysql_select_db ("Base64imgdb");

$path = "./";
$dir_handle = Opendir ($path) or Die ("Unable to open Directory $path");
while ($file = Readdir ($dir_handle)) {
$filetyp = substr ($file,-3);
if ($filetyp = = ' gif ' OR $filetyp = = ' jpg ') {
$handle = fopen ($file, ' R ');
$file_content = Fread ($handle,filesize ($file));
Fclose ($handle);
$encoded = Chunk_split (Base64_encode ($file_content));
$sql = "INSERT into images SET sixfourdata= ' $encoded '";
mysql_query ($sql);
}
}

Closedir ($dir_handle);
Echo ("complete");
Mysql_close ($DBCNX);
?>
image.php:
?
$DBCNX = mysql_connect ("localhost", "username", "password");

mysql_select_db ("Base64imgdb");

$img = $_request["img"];

$result = mysql_query ("SELECT * from Images WHERE imgid=".) $img. "");

if (!$result) {

Echo ("<b>error performing Query:".) Mysql_error (). "</b>");
Exit ();
}
while ($row = Mysql_fetch_array ($result)) {
$imgid = $row["Imgid"];
$encodeddata = $row["Sixfourdata"];
}
Mysql_close ($DBCNX);
echo Base64_decode ($encodeddata);
?>
and view.php (i shouldnt need to post this ...)
<body>
..

..
</body>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.