码迷,mamicode.com
首页 > Web开发 > 详细

php篇一 之上传文件

时间:2018-04-28 11:49:21      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:nbsp   where   gre   info   []   link   set   return   ror   

其实上传文件明白了道理也就很简单啦,小编当时学的时候也是一头雾水,搞了好多天才弄懂一点,好了废话少说吧,嘿嘿。首先运用的是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篇一 之上传文件

标签:nbsp   where   gre   info   []   link   set   return   ror   

原文地址:https://www.cnblogs.com/skyyuan/p/8966435.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!