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

本文介绍了一个使用Yii框架辅助类FileHelper的方法来生成图片上传路径的PHP函数。该函数首先获取临时文件名并计算其MD5散列值作为唯一标识符,然后根据此标识符构造出最终的图片存储路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值