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.youkuaiyun.com/qq_26656329/article/details/51852509
本文介绍了一个基于Yii框架的图片上传功能实现方法,并详细解释了如何使用FileHelper类来创建目录,确保图片能够成功上传到指定路径。此外,还提供了一个具体的getUploadedImagePath方法示例。
788

被折叠的 条评论
为什么被折叠?



