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:
02 |
< HEAD >< TITLE >Store binary data into SQL Database</ TITLE ></ HEAD > |
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‘]; |
15 |
$ connect = MYSQL_CONNECT ( "localhost", "root", "") or die("Unable to connect to MySQL server"); |
16 |
mysql_select_db( "test") or die("Unable to select database"); |
18 |
$ data = addslashes (fread(fopen($form_data, "r"), filesize($form_data))); |
20 |
//echo "mysqlPicture=".$data; |
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‘)"); |
25 |
$ id = mysql_insert_id (); |
26 |
print "<p>This file has the following Database ID: < a href = ‘getdata.php?id=$id‘ >< b >$id</ b ></ a >"; |
35 |
< form method = "post" action = "http://localhost/temp/1018/upload.php" enctype = "multipart/form-data" > |
PNS |
<input type = "text" name = "form_description" size = "" " > |
38 |
< INPUT TYPE = "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 >< input type = "submit" name = "submit" value = "submit" > |
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.