php图片上传代码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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  
 }  
 ?>

 

以上是关于php图片上传代码的主要内容,如果未能解决你的问题,请参考以下文章

php文件上传漏洞代码只允许上传图片

php上传图片,网站代码

php表单上传图片到七牛云存储并返回地址……求具体流程~有代码更好

php图片上传代码

php过滤上传类型,只上传图片类型文件

php上传图片代码