相關(guān)關(guān)鍵詞
關(guān)于我們
最新文章
- PHP中opcode緩存簡(jiǎn)單用法分析
- thinkPHP控制器變量在模板中的顯示方法示例
- PHP move_uploaded_file() 函數(shù)(將上傳的文件移動(dòng)到新位置)
- dirname(__FILE__)的含義和應(yīng)用說(shuō)明
- thinkPHP5框架實(shí)現(xiàn)分頁(yè)查詢功能的方法示例
- PHP中單雙號(hào)與變量
- PHP獲得當(dāng)日零點(diǎn)時(shí)間戳的方法分析
- Laravel ORM對(duì)Model::find方法進(jìn)行緩存示例詳解
- PHP讀寫(xiě)文件高并發(fā)處理操作實(shí)例詳解
- 【CLI】利用Curl下載文件實(shí)時(shí)進(jìn)度條顯示的實(shí)現(xiàn)
yii2.0整合阿里云oss刪除單個(gè)文件的方法
主要思路,在controller里邊通過(guò)獲得要?jiǎng)h除的文件fileid,把fileid傳遞給model的deletefile方法進(jìn)行處理,并返回處理結(jié)果。
在deletefile方法里邊,先根據(jù)id找到對(duì)應(yīng)的文件信息,然后刪除數(shù)據(jù)里邊的記錄和阿里云oss里邊的文件和本地的文件。
主要步驟如下:
1 首先是構(gòu)建一個(gè)view視圖,或者寫(xiě)一個(gè)ajax發(fā)送請(qǐng)求,此處代碼略(讓前端去寫(xiě)吧)。
2 在控制器里邊接收參數(shù),并轉(zhuǎn)交給model??刂破鱑ploadController.php里邊的代碼參考如下:
/** * 根據(jù)fileid刪除文件 * @return mixed 刪除是否成功 */ public function actionDeletefile() { $res['error'] = 1; // 準(zhǔn)備返回?cái)?shù)據(jù) Yii::$app->response->format = Response::FORMAT_JSON; // 設(shè)置返回格式 if (Yii::$app->request->isPost){ // 如果是post請(qǐng)求 $postdata = Yii::$app->request->post('fileid',0); // 從post里邊獲取文件id if ($postdata==0){ // 如果文件id為0 $res['errmsg'] = '刪除失敗,請(qǐng)重試'; // 準(zhǔn)備返回?cái)?shù)據(jù) return $res; // 返回結(jié)果 } $model = new UploadForm(); // 實(shí)例化model $delres = $model->deletefile($postdata); // 根據(jù)id調(diào)用deletefile方法 if ($delres['error']==0){ // 如果刪除成功 $res['error'] = 0; // 準(zhǔn)備返回信息 } else { $res['errmsg'] = $delres['errmsg']; // 如果刪除失敗,準(zhǔn)備返回信息 } } else { $res['errmsg'] = '非法請(qǐng)求'; // 主要考慮post請(qǐng)求,get請(qǐng)求請(qǐng)自行修改代碼 } return $res; // 返回刪除結(jié)果 }