php篇一 之上传文件

Posted skyyuan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php篇一 之上传文件相关的知识,希望对你有一定的参考价值。

其实上传文件明白了道理也就很简单啦,小编当时学的时候也是一头雾水,搞了好多天才弄懂一点,好了废话少说吧,嘿嘿。首先运用的是tp3.2的框架,我这里前端名字是img.html

所以后台首先运用的是public  function img(),然后选择上传的商品种类,再根据种类来选择固定的函数,而upload也就是确定所上传文件的格式还有保存路径等。最后再根据db_img上传到数据库。

首先是前端代码

<!DOCTYPE html>
<html>
<head>
 <title>后台管理</title>
 <link rel="stylesheet" type="text/css" href="__PUBLIC__/css/img.css"/>
</head>
<body>
  <header>
   <h1><img src="__PUBLIC__/image/admin_logo.png"/></h1>
   <ul class="rt_nav">
    <li><a href="{:U(‘Login/img‘)}" target="_blank" class="website_icon">站点首页</a></li>
    <li><a href="{:U(‘Login/order2‘)}" class="admin_icon">查看交易</a></li>
    <li><a href="{:U(‘Login/loginprocess‘)}" class="quit_icon">安全退出</a></li>
   </ul>
  </header>
<aside class="lt_aside_nav content mCustomScrollbar">
 <h2><a href="index.php">起始页</a></h2>
 <ul>
  <li>
   <dl>
    <dt>商品信息</dt>
    <!--当前链接则添加class:active-->
    <dd><a href="{:U(‘Login/img‘)}" >商品列表</a></dd>
    <dd><a href="{:U(‘Login/img‘)}" >商品分类</a></dd>
    <dd><a href="{:U(‘Login/img‘)}" >商品列表</a></dd>
    <dd><a href="{:U(‘Login/img‘)}" >商品信息</a></dd>
   </dl>
  </li>
  <li>
   <dl>
    <dt>订单信息</dt>
    <dd><a href="{:U(‘Login/order2‘)}">订单列表</a></dd>
    <dd><a href="{:U(‘Login/order3‘)}">用户购物车列表</a></dd>
   </dl>
  </li>
  <li>
   <p class="btm_infor">? 八号当铺  版权所有</p>
  </li>
 </ul>
</aside>

    <h2><strong style="color:grey;text-align: center;" >欢迎<span style="color:red">{$_SESSION[‘username‘]}</span>成为卖家</strong></h2>
     <section>
      <h2><strong style="color:grey;">卖家上传</strong></h2>
      <form method="POST" enctype="multipart/form-data">
      <ul class="ulColumn2">
       <li>
        <span class="item_name" style="width:120px;">商品名称:</span>
        <input type="text" class="textbox textbox_295" placeholder="文本信息提示..." name="f_name"/>
        <!-- <span class="errorTips">错误提示信息...</span> -->
       </li>
        <li>
        <span class="item_name" style="width:120px;">商品价格:</span>
        <input type="text" class="textbox textbox_295" placeholder="文本信息提示..." name="money" />
        <!-- <span class="errorTips">错误提示信息...</span> -->
       </li>
       <li>
        <span class="item_name" style="width:120px;">种类:</span>
        <select class="select" name="sel">
         <option value="close">服装</option>
         <option value="book">书刊</option>
         <option value="dian">电子</option>
        </select>
        <!-- <span class="errorTips">错误提示信息...</span> -->
       </li>
   <!--     <li>
        <span class="item_name" style="width:120px;">是否热销:</span>
        <label class="single_selection" ><input type="radio" value="y" />推荐</label>
        <label class="single_selection"><input type="radio" value="n" />不推荐</label>
       </li> -->
       <li>
        <span class="item_name" style="width:120px;">摘要:</span>
        <textarea placeholder="摘要信息" class="textarea" style="width:500px;height:100px;" name="message"></textarea>
       </li>
       <li>
        <span class="item_name" style="width:120px;">上传图片:</span>
        <label class="uploadImg">
         <input type="file" name="file[]" />
         <span>上传图片</span>
        </label>
       </li>
       <li>
        <span class="item_name" style="width:120px;"></span>
        <input type="submit" value="提交" class="link_btn"/>
       </li>
      </ul>
      </form>
     </section>
     
</body>
</html> 

  


其次是后台(运用的是tp3.2框架)

// //后台添加图片
public function upload()
{
  //   //实例化上传类
  $upload           = new \Think\Upload();
  $upload->maxSize  = 3145728;
  $upload->exts     = [‘jpg‘, ‘jpeg‘, ‘gif‘, ‘png‘];
  $upload->savePath = ‘./Public/Uploads/‘;
  $upload->rootPath = ‘./‘;
  $info             = $upload->upload();
  $upload           = new Upload();
  
  if (!$info) {
    $this->error($upload->getError());
    // $this->error(‘shuangchuanshibai ‘,U(‘Login/loginss‘),2);
  } else {
    //$this->success(‘上传成功‘);
    return $info;
  }
}

public function insert()
{
  $login  = D(‘logins‘);
  $result = $login->where([‘username‘ => $_SESSIN[‘username‘]])->field()->find();
  // if(empty($_FILES)){
  if (empty($_FILES)) {
    $this->error(‘请选择需要上传的文件‘);
  }
  if (!empty($_FILES)) {
    $arr = $this->upload();
    foreach ($arr as $key => $value) {
      $path[][‘savepath‘] = $value[‘savepath‘] . $value[‘savename‘];
    }
    // $data[‘single‘]=‘推荐‘;
    $data[‘f_name‘] = $_POST[‘f_name‘];
    // $data[‘create_time‘]=time();
    $data[‘money‘]    = $_POST[‘money‘];
    $data[‘message‘]  = $_POST[‘message‘];
    $data[‘username‘] = $_SESSION[‘username‘];
    // $data[‘create_time‘] = time
    // $data[‘login_id‘] = $result[‘id‘];();
    
    if (isset($data)) {
      //如果上传文件的信息不为空,我们就将这些信息保存到数据库中
      $this->db_img($data, $path);
      
    } else {
      $this->error(‘数据为空‘);
    }
  }
}

public function db_img($data, $path)
{
  // if($_POST[‘sel‘]==‘close‘){
  //         if($_POST[‘single‘]==‘y‘){
  $file = D(‘file‘);
  $img  = D(‘file_c‘);
  foreach ($path as $vo) {
    $data[‘savepath‘] = $vo[‘savepath‘];
  }
  // $data[‘single‘] ="推荐";
  $data[‘f_name‘]      = $_POST[‘f_name‘];
  $data[‘username‘]    = $_SESSION[‘username‘];
  $t                   = time();
  $data[‘create_time‘] = date("Y-m-d H:i:s", $t);
  $data[‘money‘]       = $_POST[‘money‘];
  $data[‘message‘]     = $_POST[‘message‘];
  // $img->add();
  if (!$img->create($data) || !$file->create($data)) {
    $this->error(‘上传失败‘);
  } else {
    if (!$img->add($data) || !$file->add($data)) {
      $this->error(‘上传失败‘);
    }
  }
  
  $this->success(‘上传成功....‘, U(‘Login/img‘));
}

//insert1
public function insert1()
{
  $login  = D(‘logins‘);
  $result = $login->where([‘username‘ => $_SESSIN[‘username‘]])->field()->find();
  // if(empty($_FILES)){
  if (empty($_FILES)) {
    $this->error(‘请选择需要上传的文件‘);
  }
  if (!empty($_FILES)) {
    $arr = $this->upload();
    foreach ($arr as $key => $value) {
      $path[][‘savepath‘] = $value[‘savepath‘] . $value[‘savename‘];
    }
    $data[‘f_name‘]   = $_POST[‘f_name‘];
    $data[‘money‘]    = $_POST[‘money‘];
    $data[‘message‘]  = $_POST[‘message‘];
    $data[‘username‘] = $_SESSION[‘username‘];
    
    if (isset($data)) {
      //如果上传文件的信息不为空,我们就将这些信息保存到数据库中
      $this->db_img1($data, $path);
      
    } else {
      $this->error(‘数据为空‘);
    }
  }
}

public function db_img1($data, $path)
{
  // if($_POST[‘sel‘]==‘close‘){
  //         if($_POST[‘single‘]==‘y‘){
  $img  = D(‘file_b‘);
  $file = D(‘file‘);
  foreach ($path as $vo) {
    $data[‘savepath‘] = $vo[‘savepath‘];
  }
  $data[‘f_name‘]      = $_POST[‘f_name‘];
  $data[‘username‘]    = $_SESSION[‘username‘];
  $t                   = time();
  $data[‘create_time‘] = date("Y-m-d H:i:s", $t);
  $data[‘money‘]       = $_POST[‘money‘];
  $data[‘message‘]     = $_POST[‘message‘];
  // $img->add($data);
  if (!$img->create($data) || !$file->create($data)) {
    $this->error(‘上传失败‘);
  } else {
    if (!$img->add($data) || !$file->add($data)) {
      $this->error(‘上传失败‘);
    }
  }
  $this->success(‘上传成功....‘, U(‘Login/img‘));
}

//insert2
public function insert2()
{
  $login  = D(‘logins‘);
  $result = $login->where([‘username‘ => $_SESSIN[‘username‘]])->field()->find();
  // if(empty($_FILES)){
  if (empty($_FILES)) {
    $this->error(‘请选择需要上传的文件‘);
  }
  if (!empty($_FILES)) {
    $arr = $this->upload();
    foreach ($arr as $key => $value) {
      $path[][‘savepath‘] = $value[‘savepath‘] . $value[‘savename‘];
    }
    $data[‘f_name‘]   = $_POST[‘f_name‘];
    $data[‘money‘]    = $_POST[‘money‘];
    $data[‘message‘]  = $_POST[‘message‘];
    $data[‘username‘] = $_SESSION[‘username‘];
    
    if (isset($data)) {
      //如果上传文件的信息不为空,我们就将这些信息保存到数据库中
      $this->db_img2($data, $path);
      
    } else {
      $this->error(‘数据为空‘);
    }
  }
}

public function db_img2($data, $path)
{
  // if($_POST[‘sel‘]==‘close‘){
  //         if($_POST[‘single‘]==‘y‘){
  $img  = D(‘file_d‘);
  $file = D(‘file‘);
  foreach ($path as $vo) {
    $data[‘savepath‘] = $vo[‘savepath‘];
  }
  $data[‘f_name‘]      = $_POST[‘f_name‘];
  $data[‘username‘]    = $_SESSION[‘username‘];
  $t                   = time();
  $data[‘create_time‘] = date("Y-m-d H:i:s", $t);
  $data[‘money‘]       = $_POST[‘money‘];
  $data[‘message‘]     = $_POST[‘message‘];
  // $img->add($data);
  if (!$img->create($data) || !$file->create($data)) {
    $this->error(‘上传失败‘);
  } else {
    if (!$img->add($data) || !$file->add($data)) {
      $this->error(‘上传失败‘);
    }
  }
  $this->success(‘上传成功....‘, U(‘Login/img‘));
}

public function img()
{
  
  if (IS_POST) {
    if ($_POST[‘sel‘] == ‘close‘) {
      $this->insert();
    }
    if ($_POST[‘sel‘] == ‘book‘) {
      $this->insert1();
    }
    
    if ($_POST[‘sel‘] == ‘dian‘) {
      $this->insert2();
    }
  }
  $this->display();
}

  



以上是关于php篇一 之上传文件的主要内容,如果未能解决你的问题,请参考以下文章

2020/1/30 PHP代码审计之文件上传漏洞

关于PHP上传文件时配置 php.ini 中的 upload_tmp_dir

java Ftp上传创建多层文件的代码片段

thinkphp 检测上传的图片中是否含有木马脚本

Amazon AWS 使用心得之图片压缩(升级篇一)

Linux服务之nginx服务篇一(概念)