建表:
CREATE TABLE Images ( PicNum int NOT NULL AUTO_INCREMENT PRIMARY KEY, Image BLOB );
我们假定文件上传域的名称为Picture:
<?
If($Picture != "none") { $PSize = filesize($Picture);
$mysqlPicture = addslashes(fread(fopen($Picture, "r"), $PSize));
mysql_connect($host,$username,$password) or die("Unable to connect to SQL server");
@mysql_select_db($db) or die("Unable to select database");
mysql_query("INSERT INTO Images (Image) VALUES '($mysqlPicture')")
or die("Can't Perform Query");
}
else
{ echo"You did not upload any picture"; }
?>
第二个文件则被用来从数据库中实际输出文件流,作为<IMG>标签的SRC属性。
第一个文件的简单形式可以如下: <HTML>
<BODY>
<?
mysql_connect($host,$username,$password)
or die("Unable to connect to SQL server");
@mysql_select_db($db) or die("Unable to select database");
$result=mysql_query("SELECT * FROM Images") or die("Can't Perform Query");
While($row=mysql_fetch_object($result)) {
echo "<IMG SRC=/"Second.php3? PicNum=$row->PicNum/">";
}
?>
</BODY>
</HTML>
当HTML页面被浏览时,每显示一副图片就会调用一次Second.php3文件。当第二个文件被调用时会传入相应的Picture ID,我们可以借此从数据库中取回对应的图片并显示。
Second.php3文件如下:
<?
$result=mysql_query("SELECT * FROM Images WHERE PicNum=$PicNum")
or die("Can't perform Query"); $row=mysql_fetch_object($result);
Header( "Content-type: image/gif"); echo $row->Image;
?>
我们假定文件上传域的名称为Picture:
<?
If($Picture != "none") { $PSize = filesize($Picture);
$mysqlPicture = addslashes(fread(fopen($Picture, "r"), $PSize));
mysql_connect($host,$username,$password) or die("Unable to connect to SQL server");
@mysql_select_db($db) or die("Unable to select database");
mysql_query("INSERT INTO Images (Image) VALUES '($mysqlPicture')")
or die("Can't Perform Query");
}
else
{ echo"You did not upload any picture"; }
?>
第二个文件则被用来从数据库中实际输出文件流,作为<IMG>标签的SRC属性。
第一个文件的简单形式可以如下: <HTML>
<BODY>
<?
mysql_connect($host,$username,$password)
or die("Unable to connect to SQL server");
@mysql_select_db($db) or die("Unable to select database");
$result=mysql_query("SELECT * FROM Images") or die("Can't Perform Query");
While($row=mysql_fetch_object($result)) {
echo "<IMG SRC=/"Second.php3? PicNum=$row->PicNum/">";
}
?>
</BODY>
</HTML>
当HTML页面被浏览时,每显示一副图片就会调用一次Second.php3文件。当第二个文件被调用时会传入相应的Picture ID,我们可以借此从数据库中取回对应的图片并显示。
Second.php3文件如下:
<?
$result=mysql_query("SELECT * FROM Images WHERE PicNum=$PicNum")
or die("Can't perform Query"); $row=mysql_fetch_object($result);
Header( "Content-type: image/gif"); echo $row->Image;
?>