yii2 根据文件生成hash格式图片目录并返回绝对路径

来源:互联网 发布:java移上移下置顶 编辑:程序博客网 时间:2024/05/17 04:53

use yii\helpers\FileHelper;

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);        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;    }

//@attrchmentpath = E:\UPUPW_AP5.5\vhosts\www.hosts.my/uploads\;
//DIRECTORY_SEPARATOR = \

调用方法
if(FileHelper::createDirectory(dirname(this->getUploadedImagePath()))){  
                print_r(
this->getUploadedImagePath());
}

返回格式
E:\UPUPW_AP5.5\vhosts\www.hosts.my/uploads\image\54\4d\544d00040884fdf9e1722785fa231f00.jpg

0 0
原创粉丝点击