photo表说明 CREATE TABLE `photo` (
`id` SMALLINT( 255 ) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`data` LONGBLOB NOT NULL ,
`type` VARCHAR( 30 ) NOT NULL
) ;
<?
if(!isset($_GET['mode'])) $mode = 'form';//默认处理
else $mode = $_GET['mode'];
switch($mode){
case 'read'://显示图片
$id = $_GET['id'];
$conn = mysql_connect("localhost","root","");
mysql_select_db("test",$conn);
$sql = "select type,data from photo where id='$id'";
$rst = mysql_query($sql,$conn);
if($row=mysql_fetch_row($rst)){
header("Content-Type:" . $row[0]);
echo $row[1];
}
mysql_free_result($rst);
break;
case 'form':
?>
<html>
<head><title>图片存储数据库一例</title></head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>?mode=store" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
文件:<input type="file" name="file">
<input type="submit" value="上传">
</form>
</body>
</html>
<?
break;
case 'store'://存储图片
echo '<html>';
echo '<head><title>图片存储数据库一例</title></head>';
echo '<body>';
echo '<a href="' . $_SERVER['PHP_SELF'] . '?mode=form">上传图片</a>';
$conn = mysql_connect("localhost","root","");
mysql_select_db("test",$conn);
//echo $file;
$file = $_FILES['file']['tmp_name'];
$type = $_FILES['file']['type'];//文件类型
$data = addslashes(fread(fopen($file,"r"),filesize($file)));
$sql = "insert into photo(data,type)
values('$data','$type')";
mysql_query($sql,$conn);
$id = mysql_insert_id();
echo "<hr>你上传的图片效果:
";
echo '<img src="' . $_SERVER['PHP_SELF'] . '?mode=read&id=' . $id . '">';
mysql_close($conn);
echo '</body>';
echo '</html>';
break;
default:
echo "要么上传要么不上传";
break;
}
?>
(非常全面的一个php技术网站, 有相当丰富的文章和源代码.)
|