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