Use PHP to upload images to the database

Source: Internet
Author: User
Today we will teach you how to upload images to the MYSQL database using PHP. In this tutorial, we need to create three php files: readdir. php
 

Today we will teach you how to upload images to the MYSQL database using PHP. In this tutorial, we need to create three php files:

Readdir. php-code for storing images in the database
Image. php-code for displaying the actual image
View. php-show how you call the image code in the 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 ");
?>
'Open a directory
"./"
The 'readdir. php file is located in this directory:
$ Path = "./";
$ Dir_handle = opendir ($ path) or die ("Unable to open directory $ path ");

The following is a difficult part. you need to take a good look at this: classify images and read some of the data in use.

Fopen

'Conversion

Base64_encode

'Insert to 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 directory and process it:

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

Code for reading images: IMAGE. PHP
This code is difficult. let's take a look.

$ Dbcnx = mysql_connect ("localhost", "username", "password ");
Mysql_select_db ("base64imgdb ");
?>
The image. php code used for reading images? Img = x:
$ Img = $ _ REQUEST ["img"];
?>
Then we need to connect to the database and read
$ 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"];
$ 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



Let's look at a 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 (" Error configurming query: ". mysql_error ()."");
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 ..)


..

..

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.