yii2.0整合阿里云oss删除单个文件的方法

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了yii2.0整合阿里云oss删除单个文件的方法脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

主要思路,在controller里边通过获得要删除文件fileid,把fileid传递给model的deletefile方法进行处理,并返回处理结果。 在deletefile方法里边,先根据id找到对应的文件信息,然后删除数据里边的记录和阿里oss里边的文件和本地的文件

主要步骤如下:

1 首先是构建一个view视图,或者一个ajax发送请求,此处代码略(让前端去写吧)。

2 在控制器里边接收参数,并转交给model。控制器UploadController.PHP里边的代码参考如下:

response->format = Response::FORMAT_JSON;  // 设置返回格式
    if (Yii::$app->request->isPost){            // 如果是post请求
      $postdata = Yii::$app->request->post('fileid',0);  // 从post里边获取文件id
  if ($postdata==0){                 // 如果<a href="https://www.js-code.COM/tag/wenjian/" target="_blank" class="keywords">文件</a>id为0
    $res['errmsg'] = '<a href="https://www.js-code.com/tag/shanchu/" target="_blank" class="keywords">删除</a>失败,请重试';        // 准备返回数据
    return $res;                  // 返回结果
  }
  $model = new UploaDForm();             // 实例化model
  $delres = $model->deletefile($postdata);      // 根据id<a href="https://www.js-code.com/tag/diaoyong/" target="_blank" class="keywords">调用</a>deletefile<a href="https://www.js-code.com/tag/fangfa/" target="_blank" class="keywords">方法</a>

  if ($delres['error']==0){              // 如果<a href="https://www.js-code.com/tag/shanchu/" target="_blank" class="keywords">删除</a>成功
    $res['error'] = 0;               // 准备返回信息
  } else {
    $res['errmsg'] = $delres['errmsg'];       // 如果<a href="https://www.js-code.com/tag/shanchu/" target="_blank" class="keywords">删除</a>失败,准备返回信息
  }

} else {
  $res['errmsg'] = '非法请求';             // 主要考虑post请求,get请求请自行<a href="https://www.js-code.com/tag/xiugai/" target="_blank" class="keywords">修改</a><a href="https://www.js-code.com/tag/daima/" target="_blank" class="keywords">代码</a>
}

return $res;                      // 返回<a href="https://www.js-code.com/tag/shanchu/" target="_blank" class="keywords">删除</a>结果

}

脚本宝典总结

以上是脚本宝典为你收集整理的yii2.0整合阿里云oss删除单个文件的方法全部内容,希望文章能够帮你解决yii2.0整合阿里云oss删除单个文件的方法所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。