加入收藏 | 设为首页 | 会员中心 | 我要投稿 焦作站长网 (https://www.0391zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-03-21 00:38:52 所属栏目:PHP教程 来源:互联网
导读:图片存储与浏览一例(Linux+Apache+PHP+MySQL)
注意本程序使用的表结构为:
    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";
      $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=http://www.jb51.net/article/"' . $PHP_SELF . '?cmd=list">显示所有图片</a>';
      echo "    ";
      echo '<a href=http://www.jb51.net/article/"' . $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=http://www.jb51.net/article/"' . $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=http://www.jb51.net/article/"<?echo $PHP_SELF;?>?cmd=store" method=http://www.jb51.net/article/"post" enctype=http://www.jb51.net/article/"multipart/form-data">
<input type=http://www.jb51.net/article/"hidden" name=http://www.jb51.net/article/"MAX_FILE_SIZE" value=http://www.jb51.net/article/"2097152">
描述:<br>
<textarea name=http://www.jb51.net/article/"description" rows=http://www.jb51.net/article/"5" cols=http://www.jb51.net/article/"100"></textarea><br>
文件:<input type=http://www.jb51.net/article/"file" name=http://www.jb51.net/article/"file"><br>
<input type=http://www.jb51.net/article/"submit" value=http://www.jb51.net/article/"上传">
</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=http://www.jb51.net/article/"' . $PHP_SELF . '?cmd=list">显示所有图片</a>';
      echo "    ";
      echo '<a href=http://www.jb51.net/article/"' . $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=http://www.jb51.net/article/"' . $PHP_SELF . '?cmd=read&id=' . $id . '">';
      mysql_close($server) or die("无法与数据库服务器断开连接");
      echo '</body>';
      echo '</html>';
      break;
}
?>

(编辑:焦作站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读