tp5新增 上傳圖片
阿新 • • 發佈:2018-12-30
<form class="form-horizontal" action="{:url('index/Goods/addg')}" method="post" enctype="multipart/form-data"> <div class="form-group"> <label class="col-sm-3 control-label no-padding-right" for="form-field-1"> 使用者名稱 </label> <div class="col-sm-9"> <input type="text" id="form-field-1" placeholder="水果名稱" class="col-xs-10 col-sm-5" name="f_name" /> </div> </div> <div class="space-4"></div> <div class="form-group"> <label class="col-sm-3 control-label no-padding-right" for="form-field-2"> 水果圖片 </label> <div class="col-sm-9"> <input type="file" id="form-field-2" placeholder="水果圖片" class="col-xs-10 col-sm-5" name="img"/> </div> </div> <div class="space-4"></div> <div class="form-group"> <label class="col-sm-3 control-label no-padding-right" for="form-field-2"> 所屬分類</label> <div class="col-sm-9"> <select name="t_id" > {foreach name='data' item='vo'} <option value="{$vo.t_id}"> {$vo.t_name}</option> {/foreach} </select> </div> </div> <div class="form-group"> <label class="col-sm-3 control-label no-padding-right" for="form-field-2"> 水果重量</label> <div class="col-sm-9"> <input type="text" placeholder="多少/g或者幾/個" class="col-xs-10 col-sm-5" name="f_weight"/> </div> </div> <div class="form-group"> <label class="col-sm-3 control-label no-padding-right" for="form-field-2"> 水果庫存</label> <div class="col-sm-9"> <input type="text" placeholder="水果庫存" class="col-xs-10 col-sm-5" name="f_surplus"/> </div> </div> <div class="form-group"> <label class="col-sm-3 control-label no-padding-right" for="form-field-2"> 產地</label> <div class="col-sm-9"> <input type="text" placeholder="產地" class="col-xs-10 col-sm-5" name="f_place"/> </div> </div> <div class="form-group"> <label class="col-sm-3 control-label no-padding-right" for="form-field-2">銷售價</label> <div class="col-sm-9"> <input type="text" placeholder="銷售價" class="col-xs-10 col-sm-5" name="m_price"/> </div> </div> <div class="form-group"> <label class="col-sm-3 control-label no-padding-right" for="form-field-2">會員價</label> <div class="col-sm-9"> <input type="text" placeholder="會員價" class="col-xs-10 col-sm-5" name="v_price"/> </div> </div> <div class="form-group"> <label class="col-sm-3 control-label no-padding-right" for="form-field-2">是否上架</label> <div class="col-sm-9"> <input type="radio" name="is_show" value="0">不上架 <input type="radio" name="is_show" value="1">上架 </div> </div> <div class="form-group"> <label class="col-sm-3 control-label no-padding-right" for="form-field-2">是否參與活動</label> <div class="col-sm-9"> <input type="radio" name="is_hot" value="0">不參與 <input type="radio" name="is_hot" value="1">參與 </div> </div> <div class="clearfix form-actions"> <div class="col-md-offset-3 col-md-9"> <button class="btn btn-info" type="submit"> <i class="icon-ok bigger-110"></i> 增加 </button> <button class="btn" type="reset"> <i class="icon-undo bigger-110"></i> 重置 </button> </div> </div> <div class="hr hr-24"></div> </form> </div><!-- /span --> </div><!-- /row -->
public function index() { //查詢分類 $data= db('sg_type')->select(); $this->view->engine->layout(true); //賦值 $this->assign('data',$data); return view('index'); } //網站後臺首頁 public function addg() { $file = request()->file('img'); $data=$_POST; if(isset($file)){ // 獲取表單上傳檔案 例如上傳了001.jpg // 移動到框架應用根目錄/public/uploads/ 目錄下 $info = $file->move(ROOT_PATH . 'public/uploads'); // var_dump($info) ;die; if($info){ // 成功上傳後 獲取上傳資訊 $a=$info->getSaveName(); $imgp= str_replace("\\","/",$a); $imgpath='uploads/'.$imgp; $data['f_img']= $imgpath; }else{ // 上傳失敗獲取錯誤資訊 echo $file->getError(); } } $data['f_time']=time(); // var_dump($data);die; $num= \think\Db::table('sg_fruits')->insert($data); if($num){ $this->redirect("goods/lists"); }