How to store pictures in the database

xiaoxiao2021-03-06  86

If you want to save binary data, such as pictorial files and html files, save directly in your MySQL database, then this article is written for you! I will tell you how to store these files through the HTML form, how to access and use these files.

This article overview:

. Create a new database in MySQL

. An example of how to store files

. An example of how to access a file

Create a new Database in MySQL

First, you must create a new database in your MySQL, we will store those binary files in this database. In the example I use the following structure, in order to establish a database,

You must do the following steps:

. Enter the MySQL controller

. Enter the command "CREATE DATABASE BINARY_DATA;"

. Enter the command "use binary_data;"

. input the command

"Create Table Binary_Data (ID INT (4) Not Null Auto_Increment Primary Key, Description Char (50), Bin_Data Longblob, FileName Char (50), FileSize Char (50), FileType Char (50));" (Can't break)

If there is no accident, the database and table should be established.

An example of how to store files

With this example you can transfer files into the database via HTML form.

STORE.PHP3

// store.php3 - by Florian Dittmer <

DITTMER@gmx.net>

?>

// If the form is submitted, the code will be executed:

IF ($ SUBMIT) {

/ / Connect to the database

// (You may need to adjust the host name, username and password)

MySQL_Connect ("LocalHost", "root", "password");

MySQL_SELECT_DB ("binary_data");

$ DATA = AddSlashes (Fread (FOPEN ($ Form_Data, "R"), FileSize ($ form_data)));

$ results = mysql_query ("INSERT INTO BINARY_DATA (Description, Bin_Data, FileName, FileSize, FileType)

[Connect a line:] Values ​​($ form_description, $ data, $ form_data_name, $ form_data_size, $ form_data_type) ");

$ ID = mysql_insert_id ();

PRINT "

This File Has The Following Database ID: $ ID "; mysql_close ();} else {/ / otherwise display a form of new data?>

From: Blue forest

转载请注明原文地址:https://www.9cbs.com/read-106397.html

New Post(0)