首页 > 开发 > PHP > 正文

yii2.0整合阿里云oss上传单个文件的示例

2024-05-04 22:45:28
字体:
来源:转载
供稿:网友

上一篇文章已经介绍了如何整合阿里云oss,这一篇主要介绍上传文件到阿里云oss。

主要思路:首先文件要上传到服务器,然后把服务器里边的文件传到阿里云oss,成功以后就把文件信息写入数据库,失败了就删除服务器的文件。

主要步骤:

0 介绍几个oss的概念。

accessKeyId     ==>> 可以理解为访问阿里云oss的账号

accessKeySecret ==>> 可以理解为访问阿里云oss的密码

bucket          ==>> 可以理解为文件在保存的根目录

endPoint        ==>> 把它放在空间和ossfile中间,就组成了访问文件的url路径,也是获取阿里云图片的方式。

object          ==>> 你的文件传到了阿里云oss以后,路径是什么,叫什么名字

看截图更容易理解一些:

1 文件上传还是涉及mvc,这次从view开始,主要就是展示一个表单,用来提交文件。aliyunoss.php代码如下:

<?phpuse yii/widgets/ActiveForm;?><?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?><?= $form->field($model, 'files')->fileInput() ?>  <button>Submit</button><?php ActiveForm::end() ?>

2 在controller里边接收文件,并转交给model处理。UploadController的示例代码如下:

public function actionTestAliyun()  {    $model = new UploadForm(); // 实例化上传类    if (Yii::$app->request->isPost) {      $model->files = UploadedFile::getInstance($model,'files'); //使用UploadedFile的getInstance方法接收单个文件      $model->setScenario('upload'); // 设置upload场景      $res = $model->uploadfile(); //调用model里边的upload方法执行上传      $err = $model->getErrors(); //获取错误信息      echo "<pre>";      print_r($res); //打印上传结果      print_r($err); //打印错误信息,方便排错      exit;    }    return $this->render('aliyunoss',['model'=>$model]);  }

3 当控制器把图片转移到model文件UploadForm.php以后,就要先移动文件到服务器的上传目录,然后在移动到阿里云。代码如下:

<?php/** * Created by PhpStorm. * Description: 阿里oss上传图片 * Author: Weini * Date: 2016/11/17 0017 * Time: 上午 11:34 */namespace app/models;use Yii;use yii/base/Exception;use yii/base/Model;class UploadForm extends Model{  public $files; //用来保存文件  public function scenarios()  {    return [      'upload' => ['files'], // 添加上传场景    ];  }  public function rules(){    return [      [['files'],'file', 'skipOnEmpty' => false, 'extensions' => 'jpg, png, gif', 'mimeTypes'=>'image/jpeg, image/png, image/gif', 'maxSize'=>1024*1024*10, 'maxFiles'=>1, 'on'=>['upload']],      //设置图片的验证规则    ];  }  /**   * 上传单个文件到阿里云   * @return boolean 上传是否成功   */  public function uploadfile(){    $res['error'] = 1;    if ($this->validate()) {      $uploadPath = dirname(dirname(__FILE__)).'/web/uploads/'; // 取得上传路径      if (!file_exists($uploadPath)) {        @mkdir($uploadPath, 0777, true);      }      $ext = $this->files->getExtension();        // 获取文件的扩展名      $randnums = $this->getrandnums();          // 生成一个随机数,为了重命名文件      $imageName = date("YmdHis").$randnums.'.'.$ext;   // 重命名文件      $ossfile = 'file/'.date("Ymd").'/'.$imageName;   // 这里是保存到阿里云oss的文件名和路径。如果只有文件名,就会放到空间的根目录下。      $filePath = $uploadPath.$imageName;         // 生成文件的绝对路径      if ($this->files->saveAs($filePath)){        // 上传文件到服务器        $filedata['filename'] = $imageName;       // 准备图片信息,保存到数据库        $filedata['filePath'] = $filePath;       // 准备图片信息,保存到数据库        $filedata['ossfile'] = $ossfile;        // 准备图片信息,保存到数据库        $filedata['userid'] = Yii::$app->user->id;   // 准备图片信息,保存到数据库,这个字段必须要,以免其他用户恶意删除别人的图片        $filedata['uploadtime'] = time();        // 准备图片信息,保存到数据库        // 上边这些代码不能照搬,要根据你项目的需求进行相应的修改。反正目的就是记录上传文件的信息        // 老板,这些代码是我搬来的,没仔细看,如果出问题了,你就扣我的奖金吧^_^        $trans = Yii::$app->db->beginTransaction();   // 开启事务        try{          $savefile = Yii::$app->db->createCommand()->insert('file', $filedata)->execute(); //把文件的上传信息写入数据库          $newid = Yii::$app->db->getLastInsertID(); //获取新增文件的id,用于返回。          if ($savefile) {              // 如果插入数据库成功            $ossupload = Yii::$app->Aliyunoss->upload($ossfile, $filePath); //调用Aliyunoss组件里边的upload方法把文件上传到阿里云oss            if ($ossupload) {            // 如果上传成功,              $res['error'] = 0;         // 准备返回信息              $res['fileid'] = $newid;      // 准备返回信息              $res['ossfile'] = $ossfile;     // 准备返回信息              $trans->commit();          // 提交事务            } else {                // 如果上传失败              unlink($filePath);         // 删除服务器上的文件              $trans->rollBack();         // 事务回滚            }          }          unlink($filePath);             // 插入数据库失败,删除服务器上的文件          $trans->rollBack();             // 事务回滚        } catch(Exception $e) {             // 出了异常          unlink($filePath);             // 删除服务器上的文件          $trans->rollBack();             // 事务回滚        }      }    }    return $res;                      // 返回上传信息  }  /**   * 生成随机数   * @return string 随机数   */  protected function getrandnums()  {    $arr = array();    while (count($arr) < 10) {      $arr[] = rand(1, 10);      $arr = array_unique($arr);    }    return implode("", $arr);  }}            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表