农夫三拳 2017-09-06 17:47:29 3006次浏览 0条评论 0 0 0

单图上传

首先你的有个表单代码如下

<form id="my-awesome-dropzone" enctype="multipart/form-data" class="dropzone" action="<?php echo \yii\helpers\Url::to(['shop/upload']) ?>">
    <input name="_csrf-backend" type="hidden" value="<?php echo Yii::$app->request->csrfToken; ?>">
                        <input name="img" type="file" >    
    <button type="submit" class="btn btn-primary pull-right">提交</button>
</form>

控制器部分

//此处$name即为 img
public function actionUpload($name)
    $uploadedFile = UploadedFile::getInstanceByName($name);

    if ($uploadedFile === null || $uploadedFile->hasError) {
        return '文件不存在';
    }
    //创建时间
    $ymd = date("Ymd");

    //存储到本地的路径
    $save_path = \Yii::getAlias('@webroot') . '/uploads/title_image/' . $ymd . '/'

    //存储到数据库的地址
    $save_url = '/uploads' . '/title_image/' . $ymd . '/';

    if (!file_exists($save_path)) {
        mkdir($save_path, 0777, true);
    }
    //图片名称
    $file_name = $uploadedFile->getBaseName();
    //图片格式
    $file_ext = $uploadedFile->getExtension();
    //新文件名
    $new_file_name = date("YmdHis") . '_' . rand(10000, 99999) . '.' . $file_ext;
    //图片信息
    $uploadedFile->saveAs($save_path . $new_file_name);
    //上传完成后返回图片新地址
    return $save_url.$new_file_name;
}

多图片上传

//这里我就把上面复制过来了,说明问题即可
<form id="my-awesome-dropzone" enctype="multipart/form-data" class="dropzone" action="<?php echo \yii\helpers\Url::to(['shop/upload']) ?>">
<input name="_csrf-backend" type="hidden" value="<?php echo Yii::$app->request->csrfToken; ?>">
<input name="img" type="file[0]" >  
<input name="img" type="file[1]" >  
<input name="img" type="file[2]" > .....等等    
<button type="submit" class="btn btn-primary pull-right">提交</button>
</form>

>多图上传控制器部分
基本和上面单图相差不大,唯一不同之处是需要调用uploadFile类的getInstancesByName静态方法
此方法大概作用就是你给他传一个你 文件数组名 然后他给你返回数组(如下:) 然后遍历里面对象即可

array(3) {
  [0]=>
  object(yii\web\UploadedFile)#65 (5) {
    ["name"]=>
    string(14) "761715,106.jpg"
    ["tempName"]=>
    string(27) "C:\Windows\Temp\php7458.tmp"
    ["type"]=>
    string(10) "image/jpeg"
    ["size"]=>
    int(156011)
    ["error"]=>
    int(0)
  }
  [1]=>
  object(yii\web\UploadedFile)#66 (5) {
    ["name"]=>
    string(15) "1757642,106.jpg"
    ["tempName"]=>
    string(27) "C:\Windows\Temp\php7459.tmp"
    ["type"]=>
    string(10) "image/jpeg"
    ["size"]=>
    int(272194)
    ["error"]=>
    int(0)
  }
  [2]=>
  object(yii\web\UploadedFile)#67 (5) {
    ["name"]=>
    string(15) "1894061,106.jpg"
    ["tempName"]=>
    string(27) "C:\Windows\Temp\php745A.tmp"
    ["type"]=>
    string(10) "image/jpeg"
    ["size"]=>
    int(300248)
    ["error"]=>
    int(0)
  }
}

控制器代码

//$name 即为'file'
    public function actionUpload($name){
       $uploadedFile=UploadedFile::getInstancesByName($name);
        $img_info=[];

        foreach ($uploadedFile as $k=>$v){
            if ($v === null || $v->hasError) {
                return '文件不存在';
            }
            //创建时间
            $ymd = date("Ymd");
            //存储到本地的路径
            $save_path = \Yii::getAlias('@webroot') . '/uploads/title_image/' . $ymd . '/';
            //存储到数据库的地址
            $save_url = '/uploads' . '/title_image/' . $ymd . '/';

            if (!file_exists($save_path)) {
                mkdir($save_path, 0777, true);
            }
            //图片名称
            $file_name = $v->getBaseName();
            //图片格式
            $file_ext = $v->getExtension();
            //新文件名
            $new_file_name = date("YmdHis") . '_' . rand(10000, 99999) . '.' . $file_ext;
            //图片信息
            $v->saveAs($save_path . $new_file_name);
            $img_info[$name.'['.$k.']']=['path' => $save_path, 'url' => $save_url, 'name' => $file_name, 'new_name' => $new_file_name, 'ext' => $file_ext];
        }
        //返回一个数组 里面是每个图片的信息(如下:)
        return $img_info;
    }


array(2) {
  ["file[0]"]=>
  array(5) {
    ["path"]=>
    string(61) "C:/laragon/www/shop/backend/web/uploads/title_image/20170906/"
    ["url"]=>
    string(30) "/uploads/title_image/20170906/"
    ["name"]=>
    string(10) "761715,106"
    ["new_name"]=>
    string(24) "20170906172534_90408.jpg"
    ["ext"]=>
    string(3) "jpg"
  }
  ["file[1]"]=>
  array(5) {
    ["path"]=>
    string(61) "C:/laragon/www/shop/backend/web/uploads/title_image/20170906/"
    ["url"]=>
    string(30) "/uploads/title_image/20170906/"
    ["name"]=>
    string(10) "807203,106"
    ["new_name"]=>
    string(24) "20170906172534_83243.jpg"
    ["ext"]=>
    string(3) "jpg"
  }
}

    没有找到数据。
您需要登录后才可以评论。登录 | 立即注册