PHP在MySQL中存贮图片

存贮图片的那列的数据类型是什么?请指教。
---------------------------------------------------------------

当然可以,用BLOB类型
存放:

 1   
 2// 如果提交了表单,代码将被执行:   
 3  
 4if ($submit) {   
 5  
 6// 连接到数据库   
 7// (你可能需要调整主机名,用户名和密码)   
 8  
 9MYSQL_CONNECT( "localhost", "root", "password");   
10mysql_select_db( "binary_data");   
11  
12$data = addslashes(fread(fopen($form_data, "r"), filesize($form_data)));   
13  
14$result=MYSQL_QUERY( "INSERT INTO binary_data (description,bin_data,filename,filesize,filetype) ".   
15"VALUES ('$form_description','$data','$form_data_name','$form_data_size','$form_data_type')");   
16  
17$id= mysql_insert_id();   
18print "

<p>This file has the following Database ID: <b>$id</b>";

MYSQL_CLOSE();

} else {

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

 1  
 2<form action=" ```
 3 echo $PHP_SELF; 
 4```" enctype="multipart/form-data" method="post">   
 5File Description:<br/>
 6<input name="form_description" size="40" type="text"/>
 7<input name="MAX_FILE_SIZE" type="hidden" value="1000000"/>
 8<br/>File to upload/store in database:<br/>
 9<input name="form_data" size="40" type="file"/>
10<p><input name="submit" type="submit" value="submit"/>
11</p></form>   
12  

}

1  
2
3   
4读取:   

// getdata.php3 - by Florian Dittmer <[email protected]>
// 调用方法: 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;

};

 1  
 2\---------------------------------------------------------------   
 3  
 4可以这样建表   
 5create table images(image_id int not null primary key,   
 6image_data blob   
 7);   
 8\---------------------------------------------------------------   
 9  
10注意:blob类型最大可存储64k的图象,超过时可用longblob类型</id></[email protected]></p>
Published At
Categories with 数据库类
Tagged with
comments powered by Disqus