当前位置: 首页 > 图文教程 > 网络编程 > PHP > 图片存储与浏览一例(Linux+Apache+PHP+MySQL)

PHP
同台服务器使用缓存APC效率高于Memcached的演示代码
GBK的页面输出JSON格式的php函数
在字符串指定位置插入一段字符串的php代码
php 数组二分法查找函数代码
php htmlspecialchars加强版
支持数组的ADDSLASHES的php函数
判断是否为指定长度内字符串的php函数
php 读取文件乱码问题
PHP+ajax 无刷新删除数据
php microtime获取浮点的时间戳
php 魔术函数使用说明
php 高效率写法 推荐
PHP 学习路线与时间表
php中理解print EOT分界符和echo EOT的用法区别小结
Search File Contents PHP 搜索目录文本内容的代码
收藏的PHP常用函数 推荐收藏保存
PHP 伪静态隐藏传递参数名的四种方法
PHP实现域名whois查询的代码(数据源万网、新网)
php 用checkbox一次性删除多条记录的方法
php str_pad() 将字符串填充成指定长度的字符串

图片存储与浏览一例(Linux+Apache+PHP+MySQL)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 62 ::
收藏到网摘: n/a

注意本程序使用的表结构为:
use test;
create table image(
id int unsigned auto_increment primary key,
description text,
filename varchar(50),
filesize int,
filetype varchar(50),
filedata longblob
);
*/
//?cmd={read|list|form|store}
//检查cmd参数的合法性
switch($cmd){
case 'read':
break;
case 'list':
break;
case 'form':
break;
Case 'store':
break;
default:
$cmd = 'list';
break;
}
switch($cmd){
case 'read':
//?cmd=read&id={}
//读一个图片
$server = mysql_connect("localhost","test","") or die("无法连接数据库服务器");
mysql_select_db("test",$server) or die("无法连接数据库");
$sql = "select filetype,filedata from image where id='$id'";
$rst = mysql_query($sql,$server) or die("$sql查询出错");
if($row=mysql_fetch_row($rst)){
header("Content-Type:" . $row[0]);
echo $row[1];
}
else{
echo "没有找到该记录";
}
mysql_free_result($rst);
mysql_close($server) or die("无法与数据库服务器断开连接");
break;
case 'list':
//?cmd=list
//显示所有图片
echo '<html>';
echo '<head><title>图片存储与浏览一例</title></head>';
echo '<body>';
echo '<a href="' . $PHP_SELF . '?cmd=list">显示所有图片</a>';
echo " ";
echo '<a href="' . $PHP_SELF . '?cmd=form">上传图片</a>';
$server = mysql_connect("localhost","test","") or die("无法连接数据库服务器");
mysql_select_db("test",$server) or die("无法连接数据库");
$sql = "select id,description,filename,filetype,filesize from image";
$rst = mysql_query($sql,$server) or die("$sql查询出错");
while($row=mysql_fetch_row($rst)){
echo "<hr>";
echo "描述:" . $row[1] . "<br>";
echo "文件名:" . $row[2] . "<br>";
echo "类型:" . $row[3] . "<br>";
echo "大小:" . $row[4] . "<br>";
echo '<img src="' . $PHP_SELF . '?cmd=read&id=' . $row[0] . '">';
}
mysql_free_result($rst);
mysql_close($server) or die("无法与数据库服务器断开连接");
echo '</body>';
echo '</html>';
break;
case 'form':
?>
<html>
<head><title>图片存储与浏览一例</title></head>
<body>
<form action="<?echo $PHP_SELF;?>?cmd=store" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="2097152">
描述:<br>
<textarea name="description" rows="5" cols="100"></textarea><br>
文件:<input type="file" name="file"><br>
<input type="submit" value="上传">
</form>
</body>
</html>
<?
break;
case 'store':
//?cmd=store&description={}&file={}&file_size={}&file_type={}&file_name={}
//存储图片
echo '<html>';
echo '<head><title>图片存储与浏览一例</title></head>';
echo '<body>';
echo '<a href="' . $PHP_SELF . '?cmd=list">显示所有图片</a>';
echo " ";
echo '<a href="' . $PHP_SELF . '?cmd=form">上传图片</a>';
$server = mysql_connect("localhost","test","") or die("无法连接数据库服务器");
mysql_select_db("test",$server) or die("无法连接数据库");
$data = addslashes(fread(fopen($file,"r"),filesize($file)));
$sql = "insert into image(description,filename,filetype,filesize,filedata)
values('$description','" . basename($file_name) . "','$file_type',$file_size,'$data')";
mysql_query($sql,$server) or die("$sql执行出错");
$id = mysql_insert_id();
echo "<hr>你上传的图片效果:<br>";
echo '<img src="' . $PHP_SELF . '?cmd=read&id=' . $id . '">';
mysql_close($server) or die("无法与数据库服务器断开连接");
echo '</body>';
echo '</html>';
break;
}
?>