文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>php图片存储与浏览一例

php图片存储与浏览一例

时间:2007-02-17  来源:PHP爱好者

注意本程序使用的表结构为:
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
use test;
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
create table image(
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
id int unsigned auto_increment primary key,
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
description text,
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
filename varchar(50),
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
filesize int,
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
filetype varchar(50),
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
filedata longblob
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
);
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
*/
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68

wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
//?cmd={read|list|form|store}
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68

wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
//检查cmd参数的合法性
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
switch($cmd){
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
case 'read':
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
break;
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
case 'list':
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
break;
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
case 'form':
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
break;
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
Case 'store':
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
break;
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
default:
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
$cmd = 'list';
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
break;
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
}
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68

wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
switch($cmd){
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
case 'read':
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
//?cmd=read&id={}
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
//读一个图片
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
$server = mysql_connect("localhost","test","") or die("无法连接数据库服务器");
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
mysql_select_db("test",$server) or die("无法连接数据库");
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
$sql = "select filetype,filedata from image where id='$id'";
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
$rst = mysql_query($sql,$server) or die("$sql查询出错");
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
if($row=mysql_fetch_row($rst)){
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
header("Content-Type:" . $row[0]);
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
echo $row[1];
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
}
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
else{
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
echo "没有找到该记录";
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
}
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
mysql_free_result($rst);
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
mysql_close($server) or die("无法与数据库服务器断开连接");
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
break;
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
case 'list':
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
//?cmd=list
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
//显示所有图片
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
echo '<html>';
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
echo '<head><title>图片存储与浏览一例</title></head>';
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
echo '<body>';
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
echo '<a href="' . $PHP_SELF . '?cmd=list">显示所有图片</a>';
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
echo "&nbsp;&nbsp;&nbsp;&nbsp;";
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
echo '<a href="' . $PHP_SELF . '?cmd=form">上传图片</a>';
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
$server = mysql_connect("localhost","test","") or die("无法连接数据库服务器");
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
mysql_select_db("test",$server) or die("无法连接数据库");
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
$sql = "select id,description,filename,filetype,filesize from image";
$rst = mysql_query($sql,$server) or die("$sql查询出错");
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
while($row=mysql_fetch_row($rst)){
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
echo "<hr>";
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
echo "描述:" . $row[1] . "<br>";
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
echo "文件名:" . $row[2] . "<br>";
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
echo "类型:" . $row[3] . "<br>";
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
echo "大小:" . $row[4] . "<br>";
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
echo '<img src="' . $PHP_SELF . '?cmd=read&id=' . $row[0] . '">';
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
}
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
mysql_free_result($rst);
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
mysql_close($server) or die("无法与数据库服务器断开连接");
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
echo '</body>';
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
echo '</html>';
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
break;
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
case 'form':
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
?>
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68

wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
<html>
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
<head><title>图片存储与浏览一例</title></head>
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
<body>
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
<form action="<?echo $PHP_SELF;?>?cmd=store" method="post" enctype="multipart/form-data">
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
<input type="hidden" name="MAX_FILE_SIZE" value="2097152">
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
描述:<br>
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
<textarea name="description" rows="5" cols="100"></textarea><br>
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
文件:<input type="file" name="file"><br>
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
<input type="submit" value="上传">
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
</form>
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
</body>
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
</html>
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68

wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
<?
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
break;
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
case 'store':
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
//?cmd=store&description={}&file={}&file_size={}&file_type={}&file_name={}
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
//存储图片
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
echo '<html>';
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
echo '<head><title>图片存储与浏览一例</title></head>';
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
echo '<body>';
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
echo '<a href="' . $PHP_SELF . '?cmd=list">显示所有图片</a>';
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
echo "&nbsp;&nbsp;&nbsp;&nbsp;";
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
echo '<a href="' . $PHP_SELF . '?cmd=form">上传图片</a>';
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
$server = mysql_connect("localhost","test","") or die("无法连接数据库服务器");
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
mysql_select_db("test",$server) or die("无法连接数据库");
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
$data = addslashes(fread(fopen($file,"r"),filesize($file)));
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
$sql = "insert into image(description,filename,filetype,filesize,filedata)
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
values('$description','" . basename($file_name) . "','$file_type',$file_size,'$data')";
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
mysql_query($sql,$server) or die("$sql执行出错");
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
$id = mysql_insert_id();
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
echo "<hr>你上传的图片效果:<br>";
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
echo '<img src="' . $PHP_SELF . '?cmd=read&id=' . $id . '">';
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
mysql_close($server) or die("无法与数据库服务器断开连接");
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
echo '</body>';
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
echo '</html>';
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
break;
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
}
wwww.chin aitpower.comRBb4S2YrKzDdyPD1pyTvcar68
?>
php爱好者站 http://www.phpfans.net c/vc/c++/java.
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载