Today we teach you how to upload images to MySQL database using PHP implementation. In this tutorial we need to build 3 PHP files:
readdir.php-code to put the picture in the database
image.php-code that shows the actual picture
view.php-Shows you how to call a picture in the database code
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 some of the more difficult parts, you need to do a good research: classify the image, and read out some of the data being used
fopen
' Convert
Base64_encode
' INSERT INTO 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 settings directory, and then process:
Closedir ($dir _handle);
Echo ("complete");
Mysql_close ($DBCNX);
?>
The code to read the picture: image.php
This code is difficult, we need to take a good look
$DBCNX = mysql_connect ("localhost", "username", "password");
mysql_select_db ("Base64imgdb");
?>
We read the image using the code image.php?img=x:
$img = $_request["img"];
?>
After that we need to connect to the database and then read the
$result = mysql_query ("SELECT * from Images WHERE imgid=". $img. "");
if (! $result) {
Echo ("
Request Error: ". Mysql_error (). "");
Exit ();
}
while ($row = Mysql_fetch_array ($result)) {
$imgid = $row ["Imgid"];
http://www.bkjia.com/PHPjc/630928.html www.bkjia.com true http://www.bkjia.com/PHPjc/630928.html techarticle Today we teach you how to upload images to MySQL database using PHP implementation. In this tutorial we need to build 3 PHP files: readdir.php-Put the image into the database code imag ...