当前位置:Gxlcms > PHP教程 > PHP从页面上传图片到数据库的示例代码分享

PHP从页面上传图片到数据库的示例代码分享

时间:2021-07-01 10:21:17 帮助过:33人阅读

PHP页面

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
// include 'templates/init.php';         //什么东西
//初始化act
$_REQUEST['act']=$_REQUEST['act']?$_REQUEST['act']:'';
if($_REQUEST['act']=='add'){
 include 'templates/tupian_add.php';
 }elseif($_REQUEST['act']=='insert'){       //文件上传
  $brand=$_FILES['logo'];
  if($brand['error']==0){
   //判断用户提交的图片格式是否是我要求的格式
   $allow_type=array('image/gif','image/jpeg','image/png','image/pjpeg');
   if(in_array($brand['type'],$allow_type)){
    //符合要求
    //在判断提交的大小
    $max_size=20000000;
    if($brand['size']<=$max_size){
     //容许上传到服务器
     $new_file_name=time().mt_rand(10000,99999).strrchr($brand['name'],'.');//新文件夹
     move_uploaded_file($brand['tmp_name'],'file_photos/'.$new_file_name);
     //var_dump($new_file_name);
     $brand_logo='file_photos/'.$new_file_name;
     //var_dump($title,$content,$time);
     include 'db.class.php';
     $db=new db('localhost','root','123','fanyi');
     $sql="insert into tupian values(null,'$brand_logo')";
     if(mysql_query($sql)){
       echo "图片添加成功"; 
      //header("Refresh: 2; url=http://localhost/admin/brand.php?act=list");
      }     
     }
    }
   }  
  }

-----------------------------------------------------------------------------------------------------------------

HTML页面

<form method="post" action="tupian.php" name="theForm" enctype="multipart/form-data" onsubmit="return validate()">
<table cellspacing="1" cellpadding="3" width="100%">
 
  <tr>
    <td class="label"><a href="JavaScript:showNotice('warn_brandlogo');" title="点击此处查看提示信息">
        <img src="images/notice.gif" width="16" height="16" border="0" alt="点击此处查看提示信息"></a>图片LOGO</td>
    <td><input type="file" name="logo" id="logo" size="45">    <br /><span class="notice-span" style="display:block"  id="warn_brandlogo">
        请上传图片        </span>
    </td>
  </tr>
 
  <tr>
    <td colspan="2" align="center"><br />
      <input type="submit" class="button" value=" 确定 " />
      <input type="reset" class="button" value=" 重置 " />
    </td>
  </tr>
</table>
</form>

以上就是PHP从页面上传图片到数据库的示例代码分享的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行