试试下面的方法:
<?
..... //必要的代码段
..... //必要的代码段
..... //必要的代码段
$row=mysql_fetch_row($result);
$body = base64_decode($row["body"]);
header("Content-type:image/");
echo $body;
?>
以上的代码务必要单独保存成PHP文件,比如showpict.php
以后就在显示的页面写上如下的关键句;
<?
print"<img src=\"showpict.php?picid=$pictid\" width=\"226\" height=\"166\">";
?>
其中,$pictid存放的是图片在库中的ID值
<?
..... //必要的代码段
..... //必要的代码段
..... //必要的代码段
$row=mysql_fetch_row($result);
$body = base64_decode($row["body"]);
header("Content-type:image/");
echo $body;
?>
以上的代码务必要单独保存成PHP文件,比如showpict.php
以后就在显示的页面写上如下的关键句;
<?
print"<img src=\"showpict.php?picid=$pictid\" width=\"226\" height=\"166\">";
?>
其中,$pictid存放的是图片在库中的ID值
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));用这个例子你可以通过Html表单将文件传输到数据库中。 store.php3 // store.php3 - by Florian Dittmer ?> // 如果提交了表单,代码将被执行: 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 ('$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";
MYSQL_CLOSE();
} else { // 否则显示储存新数据的表单 ?> @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作为一个参数。 例如: 一个文件在数据库中的ID为2. 你可以这样调用它: getdata.php3?id=2