Save and output images with PHP and MySQL

Source: Internet
Author: User
Tags php and mysql

MySQL can save the binary data directly, the data type is BLOB.

Fields that are commonly used in a database, such as text or integer types, and fields that need to be used to hold pictures are different
Is that the amount of data that needs to be saved is different. MySQL database uses specialized fields to hold large volumes of data, data
Type is blob.
The MySQL database defines the blob as follows: The BLOB data type is a large binary object that can be stored
Variable amount of data. BLOBs have four types, Tinyblob,blob, Mediumblob, and Longblob, respectively, and differ in the maximum length of data they can hold .

Set up a database
CREATE TABLE Ccs_image (
ID int (4) unsigned not NULL auto_increment,
Description varchar (+) default NULL,
Bin_data Longblob,
FileName varchar () default NULL,
FileSize varchar () default NULL,
filetype varchar () default NULL,
PRIMARY KEY (ID)
)

Next is the page to upload the file, Upload.php,code as follows:

01 <HTML>
02 <HEAD><TITLE>Store binary data into SQL Database</TITLE></HEAD>
03 <BODY>
04
05 <?php
06
07
08 if (isset($_POST[‘submit‘])) {
09 $form_description = $_POST[‘form_description‘];
10 $form_data_name = $_FILES[‘form_data‘][‘name‘];
11 $form_data_size = $_FILES[‘form_data‘][‘size‘];
12 $form_data_type = $_FILES[‘form_data‘][‘type‘];
13 $form_data = $_FILES[‘form_data‘][‘tmp_name‘];
14  
15 $connectMYSQL_CONNECT( "localhost", "root", "") or die("Unable to connect to MySQL server");
16 mysql_select_db( "test") or die("Unable to select database");
17
18 $dataaddslashes(fread(fopen($form_data, "r"), filesize($form_data)));
19
20 //echo "mysqlPicture=".$data;
21
22
23 $result=MYSQL_QUERY( "INSERT INTO ccs_image (description,bin_data,filename,filesize,filetype) VALUES (‘$form_description‘,‘$data‘,‘$form_data_name‘,‘$form_data_size‘,‘$form_data_type‘)");
24
25 $idmysql_insert_id();
26 print "<p>This file has the following Database ID: <ahref=‘getdata.php?id=$id‘><b>$id</b></a>";
27
28
29 MYSQL_CLOSE();
30
31 } else {
32
33 ?>
34  <center>
35 <formmethod="post" action="http://localhost/temp/1018/upload.php"enctype="multipart/form-data">
36 File Description:
PNS <input  type = "text"  name = "form_description"  size = "" " >
38 <INPUTTYPE="hidden" name="MAX_FILE_SIZE"value="1000000"> <br>
39 File to upload/store in database:
[ <input  type = "file"  name = "Form_data"  size = "Max" >
41 <p><inputtype="submit" name="submit"value="submit">
42 </form>
43  </center>
44
45 <?php
46
47 }
48
49 ?>
50 </BODY>
51 </HTML>

in the above $_files[' form_data ' [' name ']; and so is to get information on the file just came in, PHP manual mentioned

Note: to ensure that the file upload form's properties are Enctype= "Multipart/form-data", the file cannot be uploaded.

Global Variables $_files since PHP 4.1.0 exists (replaced with $HTTP _post_files in earlier versions). This array contains all the uploaded file information.

in the above example $_files the contents of the array are as follows. We assume that the name of the File upload field is userfile, as shown in the example above. Names can be arbitrarily named.

$_files[' userfile ' [' name ']

The original name of the client machine file.

$_files[' userfile ' [' type '] the MIME type of the file for

, if the browser provides this information. An example is "Image/gif". However, this MIME type is not checked on the PHP side, so do not assume this value is assumed.

$_files[' userfile ' [' Size ']

the size of the uploaded file, in bytes.

$_files[' userfile ' [' tmp_name ']

files are uploaded after the temporary file name stored on the server.

$_files[' userfile ' [' Error ']

and the file upload related error codes . This item was added in version PHP 4.2.0.

Related Article

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.