php article one upload file

In fact, it is very simple to upload the file and understand the truth. The editor was also confused when I was learning. It took a lot of time to understand a little bit. The first thing I use is the tp3.2 framework. My front-end name here is img.html

Therefore, the public function img() is first used in the background, and then selects the type of product to be uploaded, and then selects a fixed function according to the type, and upload is to determine the format of the uploaded file and the save path, etc. Finally, upload it to the database according to db_img.

The first is the front-end code

<!DOCTYPE html>
<html>
<head>
 <title>Background management</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>Product Information</dt>
    <!--Add class:active--> to the current link
    <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>Order Information</dt>
    <dd><a href="{:U('Login/order2')}">订单列表</a></dd>
    <dd><a href="{:U('Login/order3')}">User Cart List</a></dd>
   </dl>
  </li>
  <li>
   <p class="btm_infor">© Eighth Pawnshop All Rights Reserved</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">Error message...</span> -->
       </li>
        <li>
        <span class="item_name" style="width:120px;">商品价格:</span>
        <input type="text" class="textbox textbox_295" placeholder="文本信息提示..." name="money" />
        <!-- <span class="errorTips">Error message...</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">Error message...</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>Upload image</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>

  


The second is the background (using the tp3.2 framework)

// //Add image in background
public function upload()
{
  // //Instantiate the upload class
  $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('Upload successful');
    return $info;
  }
}

public function insert()
{
  $login  = D('logins');
  $result = $login->where(['username' => $_SESSIN['username']])->field()->find();
  // if(empty($_FILES)){
  if (empty($_FILES)) {
    $this->error('Please select the file to upload');
  }
  if (!empty($_FILES)) {
    $arr = $this->upload();
    foreach ($arr as $key => $value) {
      $path[]['savepath'] = $value['savepath'] . $value['savename'];
    }
    // $data['single']='Recommended';
    $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)) {
      //If the information of the uploaded file is not empty, we will save the information to the database
      $this->db_img($data, $path);
      
    } else {
      $this->error('Data is empty');
    }
  }
}

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'] = "Recommended";
  $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('Upload failed');
  } else {
    if (!$img->add($data) || !$file->add($data)) {
      $this->error('Upload failed');
    }
  }
  
  $this->success('Upload successful....', 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('Please select the file to upload');
  }
  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)) {
      //If the information of the uploaded file is not empty, we will save the information to the database
      $this->db_img1($data, $path);
      
    } else {
      $this->error('Data is empty');
    }
  }
}

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('Upload failed');
  } else {
    if (!$img->add($data) || !$file->add($data)) {
      $this->error('Upload failed');
    }
  }
  $this->success('Upload successful....', 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('Please select the file to upload');
  }
  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)) {
      //If the information of the uploaded file is not empty, we will save the information to the database
      $this->db_img2($data, $path);
      
    } else {
      $this->error('Data is empty');
    }
  }
}

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('Upload failed');
  } else {
    if (!$img->add($data) || !$file->add($data)) {
      $this->error('Upload failed');
    }
  }
  $this->success('Upload successful....', 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();
}

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325026983&siteId=291194637