Thinkphp5 + uploadify 整合圖片上傳
阿新 • • 發佈:2018-12-17
專案中需要上傳選單的圖示,最近大家都用TP5,我也與時俱進了一把,開始自己想太多了,在返回值那裡栽了跟頭,腦袋裡一直都是json,json,這裡的返回值直接return就好了,不要去搞成json資料,這裡還是附上程式碼,給自己警示作用,說不定哪天又忘記了……
<!--前端,引用的檔案我就不多說了哈該有的js,css一定不要忘記了--> <div class="row cl"> <label class="form-label col-xs-4 col-sm-3">選單圖示:</label> <div class="formControls col-xs-8 col-sm-8"> <img src="__images__/iconImg.jpg" id="m_icon1" width="80px" /> <br /><br /> <input type="file" id="file_upload1" multiple="true"> </div> </div>
//圖示上傳,js $('#file_upload1').uploadify({ uploader:"{:url('Image/addImage')}", // 伺服器處理地址 swf: '__hui__/lib/uploadify/uploadify.swf', buttonText: "上傳圖示", //按鈕文字 height: 30, //按鈕高度 width: 80, //按鈕寬度 fileTypeExts: "*.jpg;*.png;*.jpeg;*.gif;*.bmp", //允許的檔案型別 fileTypeDesc: "請選擇", //檔案說明 fileObjName: "m_icon", onUploadSuccess: function (file, data, response) { //上傳成功後的響應事件處理 $("#m_icon1").attr("src","__upload__/iconImg/"+data); //預覽圖片 $("#add-m_icon").val(data); //將上傳後的圖片賦值給表單 } });
/* 圖片上傳 */ public function addImage(){ // 獲取表單上傳檔案 $file = request()->file('m_icon'); if(empty($file)) { $data = '請選擇上傳檔案!'; return $data; } // 移動到框架應用根目錄/public/upload/ 目錄下 $info = $file->move(ROOT_PATH.'public'.DS.'upload/iconImg/'); //獲取檔案 //$info->getFilename(); //檔名 dc269f3ad05a6e656ccbdec468b970f4.png //$info->getSaveName(); //檔名(日期/檔案)20181026/dc269f3ad05a6e656ccbdec468b970f4.png //$info->getExtension(); //檔案字尾名 png //$info->getPathname(); //檔案路徑包括磁碟名稱 if($info){ $data = $info->getSaveName(); //檔案路徑 }else{ $data = '檔案上傳失敗啦!'; } return $data; //直接返回,不要封住成json }