Home Database Mysql Tutorial 教你如何在MySQL数据库中直接储存图片_MySQL

教你如何在MySQL数据库中直接储存图片_MySQL

Jun 01, 2016 pm 02:04 PM
bi id store picture how database document program

 

本文概述:

1、在MySQL中建立一个新的数据库。

2、一个怎样储存文件的例子程序。

3、一个怎样访问文件的例子程序。

在MySQL中建立一个新的database。

首先,你必须在你的MySQL中建立一个新的数据库,我们将会把那些二进制文件储存在这个数据库里。在例子中我会使用下列结构,为了建立数据库,你必须做下列步骤:

进入MySQL控制器;

输入命令“create database binary_data;”

输入命令“use binary_data;”

输入命令:

 

<ccid_code></ccid_code>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));" 
(注:不能断行)
Copy after login

如果没有意外,数据库和表应该建立好了。

一个怎样储存文件的例子程序 用这个例子你可以通过Html表单将文件传输到数据库中。

Store.php3 以下为引用的内容:

 

<ccid_code></ccid_code><?php // store.php3 - by Florian Dittmer <dittmer@gmx.net> 
?> 

 
<title>Store binary data into SQL Database</title> 
 

<?php // 如果提交了表单,代码将被执行: 

if ($submit) { 

// 连接到数据库 
// (你可能需要调整主机名,用户名和密码) 

MYSQL_CONNECT( "localhost", "root", "password"); 
mysql_select_db( "binary_data"); 

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

$result=MYSQL_QUERY( "INSERT INTO binary_data

(description,bin_data,filename,filesize,filetype) ". 
"VALUES (&rsquo;$form_description&rsquo;,&rsquo;
$data&rsquo;,&rsquo;$form_data_name&rsquo;,&rsquo;$form_data_size&rsquo;,&rsquo;$form_data_type&rsquo;)"); 

$id= mysql_insert_id(); 
print  "<p>This file has the following Database ID: <b>$id</b>"; 

MYSQL_CLOSE(); 

} else { 

// 否则显示储存新数据的表单 
?> 

Copy after login
File Description:

File to upload/store in database:

 

如果你执行了这个程序,你将会看见一个简单的Html表单,单击“浏览”选择一个文件,然后单击提交。

当文件上传至web服务器之后,程序将会告诉你刚刚上传的文件的ID,记住这个ID,一会儿可能会用到。

一个怎样访问文件的例子程序

 

<ccid_code></ccid_code><?php // getdata.php3 - by Florian Dittmer <dittmer@gmx.net> 
// 调用方法: getdata.php3?id=<id> 

if($id) { 

// 你可能需要调整主机名,用户名和密码: 
@MYSQL_CONNECT( "localhost", "root", "password"); 

@mysql_select_db( "binary_data"); 

$query =  "select bin_data,filetype from binary_data where id=$id"; 
$result = @MYSQL_QUERY($query); 

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

Header(  "Content-type: $type"); 
echo $data; 

}; 
?></id>
Copy after login

程序必须知道要访问那个文件, 你必须将ID作为一个参数。

例如: 一个文件在数据库中的ID为2

你可以这样调用它: getdata.php3?id=2

如果你将图片储存在数据库里, 你可以向调用图片一样调用它。

例如: 一个图片文件在数据库中的ID为3. 你可以这样调用它:

 

怎样储存大于1MB的文件:

如果你想储存大于1MB的文件,你必须对你的程序、PHP设置、SQL设置进行许多修改,下面几条也许可以帮助你储存小于24MB的文件:

修改:store.php3 ,将 MAX_FILE_SIZE 的值改成 24000000。

修改:你的PHP设置,在一般情况下,PHP只允许小于2MB的文件,你必须将max_filesize(在php.ini中)的值改成24000000。 去掉MYSQL的数据包大小限制,在一般情况下 MYSQL小于1MB的数据包。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to make Google Maps the default map in iPhone How to make Google Maps the default map in iPhone Apr 17, 2024 pm 07:34 PM

The default map on the iPhone is Maps, Apple's proprietary geolocation provider. Although the map is getting better, it doesn't work well outside the United States. It has nothing to offer compared to Google Maps. In this article, we discuss the feasible steps to use Google Maps to become the default map on your iPhone. How to Make Google Maps the Default Map in iPhone Setting Google Maps as the default map app on your phone is easier than you think. Follow the steps below – Prerequisite steps – You must have Gmail installed on your phone. Step 1 – Open the AppStore. Step 2 – Search for “Gmail”. Step 3 – Click next to Gmail app

This Apple ID is not yet in use in the iTunes Store: Fix This Apple ID is not yet in use in the iTunes Store: Fix Jun 10, 2024 pm 05:42 PM

When logging into iTunesStore using AppleID, this error saying "This AppleID has not been used in iTunesStore" may be thrown on the screen. There are no error messages to worry about, you can fix them by following these solution sets. Fix 1 – Change Shipping Address The main reason why this prompt appears in iTunes Store is that you don’t have the correct address in your AppleID profile. Step 1 – First, open iPhone Settings on your iPhone. Step 2 – AppleID should be on top of all other settings. So, open it. Step 3 – Once there, open the “Payment & Shipping” option. Step 4 – Verify your access using Face ID. step

Clock app missing in iPhone: How to fix it Clock app missing in iPhone: How to fix it May 03, 2024 pm 09:19 PM

Is the clock app missing from your phone? The date and time will still appear on your iPhone's status bar. However, without the Clock app, you won’t be able to use world clock, stopwatch, alarm clock, and many other features. Therefore, fixing missing clock app should be at the top of your to-do list. These solutions can help you resolve this issue. Fix 1 – Place the Clock App If you mistakenly removed the Clock app from your home screen, you can put the Clock app back in its place. Step 1 – Unlock your iPhone and start swiping to the left until you reach the App Library page. Step 2 – Next, search for “clock” in the search box. Step 3 – When you see “Clock” below in the search results, press and hold it and

Can't allow access to camera and microphone in iPhone Can't allow access to camera and microphone in iPhone Apr 23, 2024 am 11:13 AM

Are you getting "Unable to allow access to camera and microphone" when trying to use the app? Typically, you grant camera and microphone permissions to specific people on a need-to-provide basis. However, if you deny permission, the camera and microphone will not work and will display this error message instead. Solving this problem is very basic and you can do it in a minute or two. Fix 1 – Provide Camera, Microphone Permissions You can provide the necessary camera and microphone permissions directly in settings. Step 1 – Go to the Settings tab. Step 2 – Open the Privacy & Security panel. Step 3 – Turn on the “Camera” permission there. Step 4 – Inside, you will find a list of apps that have requested permission for your phone’s camera. Step 5 – Open the “Camera” of the specified app

How does Hibernate implement polymorphic mapping? How does Hibernate implement polymorphic mapping? Apr 17, 2024 pm 12:09 PM

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

See all articles