如果你想把二進制的數(shù)據(jù),比如說圖片文件和html文件,直接保存在你的mysql數(shù)據(jù)庫,那么這篇文章就是為你而寫的!我將告訴你怎樣通過html表單來儲存這些文件,怎樣訪問和使用這些文件。
本文概述:
在mysql中建立一個新的數(shù)據(jù)庫
一個怎樣儲存文件的例子程序
一個怎樣訪問文件的例子程序
在mysql中建立一個新的database
首先,你必須在你的mysql中建立一個新的數(shù)據(jù)庫,我們將會把那些二進制文件儲存在這個數(shù)據(jù)庫里。在例子中我會使用下列結構,為了建立數(shù)據(jù)庫,你必須做下列步驟:
進入mysql控制器
輸入命令"create database binary_data;"
輸入命令"use binary_data;"
輸入命令"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));" (不能斷行)
如果沒有意外,數(shù)據(jù)庫 和 表 應該建立好了。
一個怎樣儲存文件的例子程序 用這個例子你可以通過html表單將文件傳輸?shù)綌?shù)據(jù)庫中。.
store.php3
| 以下為引用的內(nèi)容: // store.php3 - by florian dittmer ?> // 如果提交了表單,代碼將被執(zhí)行: if ($submit) { // 連接到數(shù)據(jù)庫 // (你可能需要調(diào)整主機名,用戶名和密碼) 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 (’$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"; } ?> |
| 以下為引用的內(nèi)容: // getdata.php3 - by florian dittmer // 調(diào)用方法: getdata.php3?id= if($id) { // 你可能需要調(diào)整主機名,用戶名和密碼: @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; }; ?> |
新聞熱點
疑難解答
圖片精選