Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to store pictures directly in MySQL

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article will explain in detail how to store pictures directly in MySQL. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

How to store pictures directly in MySQL

First of all, you must create a new database in your mysql, and we will store those binaries in this database. I will use the following structure in the example. In order to set up the database, you must do the following steps:

Enter the MySql controller

Enter the command "createdatabasebinary_data;"

Enter the command "usebinary_data;"

Enter the command "CREATETABLEbinary_data (idINT (4) NOTNULLAUTO_INCREMENTPRIMARYKEY"

DescriptionCHAR (50), bin_dataLONGBLOB,filenameCHAR (50), filesizeCHAR (50), filetypeCHAR (50)); "(cannot break lines)

If there are no surprises, the database and tables should be established.

An example program of how to save a file using this example you can transfer the file to the database through the Html form. .

The following are references to store.php3:

/ / store.php3-byFlorianDittmer

>

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

If ($submit) {

/ / Connect to the database

/ (you may need to adjust the hostname, user name and password)

MYSQL_CONNECT ("localhost", "root", "password")

Mysql_select_db ("binary_data")

$data=addslashes (fread (fopen ($form_data, "r"), filesize ($form_data)

Result=MYSQL_QUERY ("INSERTINTObinary_data (description,bin_data,filename,filesize,filetype)".

"VALUES ('$form_description','$data','$form_data_name','$form_data_size','$form_data_type')")

$id=mysql_insert_id ()

Print "

ThisfilehasthefollowingDatabaseID:$id "

MYSQL_CLOSE ()

} else {

/ / otherwise, display the form that stores the new data

>

Filetoupload/storeindatabase:

}

>

How to store pictures directly in MySQL

If you execute this program, you will see a simple Html form, click Browse to select a file, and then click submit.

When the file is uploaded to the web server, the program will tell you the ID of the file you just uploaded. Remember this ID, which you will use later.

An example program of how to access a file

You can access the files you just saved through this program.

The following is the referenced content:

/ / getdata.php3-byFlorianDittmer

/ / call method: getdata.php3id=

If ($id) {

/ / you may need to adjust the hostname, user name and password:

@ MYSQL_CONNECT ("localhost", "root", "password")

@ mysql_select_db ("binary_data")

$query= "selectbin_data,filetypefrombinary_datawhereid=$id"

$result=@MYSQL_QUERY ($query)

$data=@MYSQL_RESULT ($result,0, "bin_data")

$type=@MYSQL_RESULT ($result,0, "filetype")

Header ("Content-type:$type")

Echo$data

}

>

The program must know that to access that file, you must take ID as a parameter.

For example, the ID of a file in the database is 2. 0. You can call it like this:

Getdata.php3id=2 if you store an image in a database, you can call it just like you would call an image.

Example: the ID of a picture file in the database is 3. 0. You can call it like this:

How to save files larger than 1MB:

If you want to save files larger than 1MB, you must make a lot of changes to your program, PHP settings, and SQL settings.

Here are a few things that may help you save files smaller than 24MB:

Modify store.php3 to change the value of MAX_FILE_SIZE to 24000000.

Modify your PHP settings. In general, PHP only allows files smaller than 2MB. You must change the value of max_filesize (in php.ini) to 24000000.

Remove the MYSQL packet size limit, in general, MYSQL is smaller than 1MB packets.

You must restart your MYSQL with the following parameters

/ usr/local/bin/safe_mysqld-Okey_buffer=16M-Otable_cache=128-Osort_buffer=4M-Orecord_buffer=1M-Omax_allowed_packet=24M

On how to directly store pictures in MySQL to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report