Home > Backend Development > PHP Tutorial > PHP sample code sharing for uploading images from page to database

PHP sample code sharing for uploading images from page to database

黄舟
Release: 2023-03-06 15:34:01
Original
1543 people have browsed it

PHP page

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

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

HTML page

<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(&#39;warn_brandlogo&#39;);" 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>
Copy after login

The above is the detailed content of PHP sample code sharing for uploading images from page to database. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template