yii2 createDirectory 文件上传的保存文件和递归创建文件夹

use yii\helpers\FileHelper;

if ($model->load(Yii::$app->request->post())) {
            #print_r(dirname$this->getUploadedImagePath());die;
    //E:\UPUPW_AP5.5\vhosts\www.jbp9.my/uploads\image\f0\8b\f08b2856079ba5066261682d8b5ddc0c.jpg
            if(FileHelper::createDirectory(dirname($this->getUploadedImagePath()))){
                print_r($this->getUploadedImagePath());
            }
}

public function getUploadedImagePath()
    {
        $file_tmp_name = $_FILES[ucwords(Yii::$app->controller->id)]['tmp_name']['image'];
        $this->hash = hash_file('md5', $file_tmp_name);
        $this->extension = 'jpg';
        $file_name = hash_file('md5', $file_tmp_name);
        #print_r($file_name."<br/>");
        return \Yii::getAlias('@attachmentPath') . DIRECTORY_SEPARATOR . 'image' . DIRECTORY_SEPARATOR . substr($this->hash, 0, 2) . DIRECTORY_SEPARATOR . substr($this->hash, 2, 2) . DIRECTORY_SEPARATOR . $this->hash .  '.' . $this->extension;
    }

这里写图片描述

官方文档
createDirectory() public method
Creates a new directory.
This method is similar to the PHP mkdir() function except that it uses chmod() to set the permission of the created directory in order to avoid the impact of the umask setting.
boolean createDirectory( path, mode = 509, recursive=true) path string
Path of the directory to be created.
modeintegerThepermissiontobesetforthecreateddirectory. recursive boolean
Whether to create parent directories if they do not exist.
return boolean
Whether the directory is created successfully
createDirectory可递归创建目录并保存一次文件,但是保存后的文件删除后再次执行则没有保存。

三个参数
FileHelper::createDirectory(dirname($this->getUploadedImagePath(),0777,true));

param1 文件绝对路径
param2 文件权限
param3 是否创建父级目录

getUploadedImagePath()
参数见http://blog.csdn.net/qq_26656329/article/details/51852509

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值