1. 程式人生 > >php篇一 之上傳文件

php篇一 之上傳文件

width ID 商品列表 proc div lin 錯誤 input 分類

其實上傳文件明白了道理也就很簡單啦,小編當時學的時候也是一頭霧水,搞了好多天才弄懂一點,好了廢話少說吧,嘿嘿。首先運用的是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篇一 之上傳文件