php图片上传代码

<?php
 // 连接数据库
 $conn[email protected]mysql_connect("localhost","root","")  or die(mysql_error());
 @mysql_select_db(‘water‘,$conn) or die(mysql_error()); 

// 判断action
 $action = isset($_REQUEST[‘action‘])? $_REQUEST[‘action‘] : ‘‘; 

// 上传图片
if($action==‘add‘){
     $image = mysql_escape_string(file_get_contents($_FILES[‘photo‘][‘tmp_name‘]));
     $type = $_FILES[‘photo‘][‘type‘];
     $sqlstr = "insert into photo(type,binarydata) values(‘".$type."‘,‘".$image."‘)";
     @mysql_query($sqlstr) or die(mysql_error());
     header(‘location:upload_image_todb.php‘);
     exit();
 // 显示图片
 }elseif($action==‘show‘){
     $id = isset($_GET[‘id‘])? intval($_GET[‘id‘]) : 0;
     $sqlstr = "select * from photo where id=$id";
     $query = mysql_query($sqlstr) or die(mysql_error());
     $thread = mysql_fetch_assoc($query);
     if($thread){
         header(‘content-type:‘.$thread[‘type‘]);
         echo $thread[‘binarydata‘];
         exit();
     }
 }else{
 // 显示图片列表及上传表单
 ?>
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <html>
  <head>
   <meta http-equiv="content-type" content="text/html; charset=utf-8">
   <title> upload image to db demo </title>
  </head>  

  <body>
   <form name="form1" method="post" action="upload_image_todb.php" enctype="multipart/form-data">
   <p>图片:<input type="file" name="photo"></p>
   <p><input type="hidden" name="action" value="add"><input type="submit" name="b1" value="提交"></p>
   </form>  

 <?php
     $sqlstr = "select * from photo order by id desc";
     $query = mysql_query($sqlstr) or die(mysql_error());
     $result = array();
     while($thread=mysql_fetch_assoc($query)){
         $result[] = $thread;
     }
     foreach($result as $val){
         echo ‘<p><img src="upload_image_todb.php?action=show&id=‘.$val[‘id‘].‘&t=‘.time().‘" width="150"></p>‘;
     }
 ?>
 </body>
 </html>
 <?php
 }
 ?>
时间: 2024-08-22 19:27:24

php图片上传代码的相关文章

图片上传代码

client页面上传到上传到服务器并且在服务器保存 xhtml页面 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <htm

jq upload图片上传代码

/* * 图片上传 * */ function postImgData() { $("#addFileInfo input[name='file']").change(function () { console.log($(this).val()) if (!$(this).val()) { return } var fileTyle = $(this).val().split("."); console.log(fileTyle); if (fileTyle[1]

AppCan做的图片上传代码

存在AppCan里的网页 index.html <!DOCTYPE html> <html class="um landscape min-width-240px min-width-320px min-width-480px min-width-768px min-width-1024px"> <head> <title></title> <meta charset="utf-8"> <

转载图片上传预览 代码

1: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Conte

图片上传根据stream生成image

对于图片上传代码的整合 因为需要判断上传的图片的宽高是否符合尺寸,所以在最初拿到inputstream的时候,就直接获取image格式的图片 本来是想在下面的checkFile中获取的,不过直接使用System.Drawing.Image.FromStream(request.PostedFile.InputStream);的时候报错了,错误意思是说参数为空,应该是由于上面获取byte[]file的时候,关闭了stream,导致request.PostFile.InputStream也没有了,所

php 使用 wangeditor3 图片上传

就在最近,公司让我写一个后台,其中用到了富文本编辑器.自从这个富文本的出现 我就慢慢的进入了一个坑,起初不知道用什么编辑器好,看了好多好多,最后选择了.这个 wangeditor3.个人认为这个富文本很干净,还很多功能. 选择了编辑器 我就慢慢的走进了坑的道理,一步一个坎.接下来就是看代码了. 这个是wangeditor,选择一个自己喜欢的版本.我用的是3的 https://github.com/wangfupeng1988/wangEditor/releases 定义一个富文本编辑器 然后富文

spring mvc 图片上传,图片压缩、跨域解决、 按天生成目录 ,删除,限制为图片代码等相关配置

spring mvc 图片上传,跨域解决 按天生成目录 ,删除,限制为图片代码,等相关配置 fs.root=data/ #fs.root=/home/dev/fs/ #fs.root=D:/fs/ #fs.domains=182=http://172.16.100.182:18080,localhost=http://localhost:8080 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE be

spring mvc 图片上传,图片压缩、跨域解决、 按天生成文件夹 ,删除,限制为图片代码等相关配置

spring mvc 图片上传,跨域解决 按天生成文件夹 ,删除,限制为图片代码,等相关配置 fs.root=data/ #fs.root=/home/dev/fs/ #fs.root=D:/fs/ #fs.domains=182=http://172.16.100.182:18080,localhost=http://localhost:8080 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE b

服务器基于PHP CodeIgniter,Android基于Volley实现多文件/图片上传(含服务器,web版和android客户端完整代码)

问题背景:app在上传图片时,同时传递参数,支持传递多个图片.本文中的环境默认已经配好了服务器的CodeIgniter框架.事实上不使用这个框架也是可以的. 一,服务器部分 1,在controllers下的helpers新建文件upload_helper.php <?php /** * Make multifile array input complaint with CI_Upload.<br> * For use files[ ] input name you must use it