现在的位置: 首页 > 综合 > 正文

PHP给Mysql数据库中上传图片,并从Mysql数据库读取图片显示在后台窗口

2014年04月05日 ⁄ 综合 ⁄ 共 5243字 ⁄ 字号 评论关闭

后台界面代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <meta charset="utf-8">
    <title>客户案例管理</title>
	<style type="text/css">
body{ font-size:14px;}
input{ vertical-align:middle; margin:0; padding:0}
.file-box{ position:relative;width:340px}
.txt{ height:22px; border:1px solid #cdcdcd; width:180px;}
.btn{ background-color:#FFF; border:1px solid #CDCDCD;height:24px; width:70px;}
.file{ position:absolute; top:0; right:80px; height:24px; filter:alpha(opacity:0);opacity: 0;width:260px }
</style>
</head>
<body>
<h2>请上传客户案例图片</h2>
<div class="file-box">
  <form action="./deal_permit.php?type=scene" method="post" enctype="multipart/form-data">
 <input type='text' name='doc' id='textfield' class='txt' />  
 <input type='button' class='btn' value='添加图片' />
    <input type="file" name="doc" class="file" id="fileField" size="28" onchange="document.getElementById('textfield').value=this.value" />
 <input type="submit" name="submit" class="btn" value="上传" />
  </form>
</div>
    
    <div class="demo-info" style="margin-bottom:10px">
        <div class="demo-tip icon-tip"> </div>
       <!-- <div>Double click the row to begin editing.</div>-->
    </div>
    
    <table id="dg" title="客户案例管理" style="width:700px;height:250px"
            toolbar="#toolbar" pagination="true" idField="id"
            rownumbers="true" fitColumns="true" singleSelect="true">
        <thead>
            <tr>
                <th field="name" width="50" editor="{type:'validatebox',options:{required:true}}">图片名称</th>
                <th field="Time" width="50" editor="{type:'validatebox',options:{required:true}}">时间</th>
                <th field="URL" width="50" editor="text">图片地址</th>
                <th field="description" width="50" editor="{type:'validatebox',options:{required:true}}">案例描述</th>
            </tr>
        </thead>
    </table>
     <!--   
	<div id="toolbar">
   
<a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="javascript:$('#dg').edatagrid('destroyRow')">Destroy</a>
<a href="#" class="easyui-linkbutton" iconCls="icon-save" plain="true" onclick="javascript:$('#dg').edatagrid('saveRow')">Save</a>
<a href="#" class="easyui-linkbutton" iconCls="icon-undo" plain="true" onclick="javascript:$('#dg').edatagrid('cancelRow')">Cancel</a>
</div>
-->
    <script type="text/javascript">
        $(function(){
            $('#dg').edatagrid({
              //  url: 'get_scene.php',
              //  saveUrl: 'save_user.php?type=scene',
             //   updateUrl: 'update_user.php?type=scene',
              //  destroyUrl: 'destroy_user.php?type=scene'
            });
        });
    </script>
    
</body>
</html>

图片上传的PHP代码:

<?php
include '../conn.php';
//用户上传图片处理文件 
//var_dump( $_FILES["doc"]["type"]);exit;
if ((($_FILES["doc"]["type"] == "image/gif")|| ($_FILES["doc"]["type"] == "image/jpeg")|| ($_FILES["doc"]["type"] == "image/pjpeg"))&& ($_FILES["doc"]["size"] < 100000000)){ //控制允许上传的图片类型,最后的100000为允许的图片大小 
if ($_FILES["doc"]["error"] > 0){ 
echo "Return Code: " . $_FILES["doc"]["error"] . "<br />"; //出错返回 
}else{ 
/* //这是上传图片的信息,去掉前后的注释就可以看到效果. 
echo "Upload: " . $_FILES["file"]["name"] . "<br />"; 
echo "Type: " . $_FILES["file"]["type"] . "<br />"; 
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; 
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; */
switch($_GET['type']){
case permit:
  if (file_exists("userupload/" . $_FILES["doc"]["name"])){ 
  echo $_FILES["doc"]["name"] . " already exists. "; 
  }else{ 
  move_uploaded_file($_FILES["doc"]["tmp_name"],"userupload/" . $_FILES["doc"]["name"]); 
  }  
  $date=date('Ymdhis'); //得到当前时间,如;20070705163148 
  $fileName=$_FILES['doc']['name']; //得到上传文件的名字 
  $name=explode('.',$fileName); //将文件名以'.'分割得到后缀名,得到一个数组 
  $newPath=$date.'.'.$name[1]; //得到一个新的文件为'20070705163148.jpg',即新的路径 
  $oldPath=$_FILES['doc']['tmp_name']; //临时文件夹,即以前的路径 
  rename("userupload/".$fileName,"userupload/".$newPath); 
  $dec=(string)$_FILES['doc']['decration'];
  //这里可以写你的SQL语句,图片的地址是 "userupload/".$newPath 
  $upload_url="insert into upload_permit_img values(null,'".$newPath."','".date('Y-m-d')."','./admin/common/userupload/".$newPath."',null)";
  mysql_query($upload_url);
break;
case product:
  if (file_exists("product/" . $_FILES["doc"]["name"])){ 
  echo $_FILES["doc"]["name"] . " already exists. "; 
  }else{ 
  move_uploaded_file($_FILES["doc"]["tmp_name"],"product/" . $_FILES["doc"]["name"]); 
  } 
  $date=date('Ymdhis'); //得到当前时间,如;20070705163148 
  $fileName=$_FILES['doc']['name']; //得到上传文件的名字 
  $name=explode('.',$fileName); //将文件名以'.'分割得到后缀名,得到一个数组 
  $newPath=$date.'.'.$name[1]; //得到一个新的文件为'20070705163148.jpg',即新的路径 
  $oldPath=$_FILES['doc']['tmp_name']; //临时文件夹,即以前的路径 
  rename("product/".$fileName,"product/".$newPath); 
  $dec=(string)$_FILES['doc']['decration'];
  //这里可以写你的SQL语句,图片的地址是 "userupload/".$newPath 
  $upload_url="insert into upload_product_img values(null,'".$newPath."','".date('Y-m-d')."','./admin/common/product/".$newPath."',null)";
  mysql_query($upload_url);
break;
case scene:
  if (file_exists("scene/" . $_FILES["doc"]["name"])){ 
  echo $_FILES["doc"]["name"] . " already exists. "; 
  }else{ 
  move_uploaded_file($_FILES["doc"]["tmp_name"],"scene/" . $_FILES["doc"]["name"]); 
  } 
  $date=date('Ymdhis'); //得到当前时间,如;20070705163148 
  $fileName=$_FILES['doc']['name']; //得到上传文件的名字 
  $name=explode('.',$fileName); //将文件名以'.'分割得到后缀名,得到一个数组 
  $newPath=$date.'.'.$name[1]; //得到一个新的文件为'20070705163148.jpg',即新的路径 
  $oldPath=$_FILES['doc']['tmp_name']; //临时文件夹,即以前的路径 
  rename("scene/".$fileName,"scene/".$newPath); 
  //这里可以写你的SQL语句,图片的地址是 "userupload/".$newPath 
  $upload_url="insert into upload_scene_img values(null,'".$newPath."','".date('Y-m-d')."','../admin/common/scene/".$newPath."',null)";
  mysql_query($upload_url);
break;
}
?> 
<script type="text/javascript">alert('图片上传成功!!');</script>
<script>window.history.go(-1);</script>
<?php 
} 
}else{ 
echo "Invalid file"; //图片类型错误或者过大 
} 
?> 

抱歉!评论已关闭.