Using PHP to upload images to the database _php tutorial

Source: Internet
Author: User
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 ...

  • 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.