yii2 createDirectory 文件上传的保存文件和递归创建文件夹
2016-07-07 17:16
531 查看
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
相关文章推荐
- 初识ThinkPHP框架
- php处理时间过长时返回空数据
- 使用ChannelSftp.chmod方法进行赋权
- 利用PHP程序设定防止MySQL注入或HTML表单滥用
- FastCgi与PHP-fpm关系
- PHP通过header实现文本文件的下载
- Laravel timestamps 设置为unix时间戳
- yii2 根据文件生成hash格式图片目录并返回绝对路径
- PHP iconv 解决utf-8和gb2312编码转换问题
- php实现xml与json之间的相互转换功能实例
- PHP 真正多线程的使用
- Yii在save时更新字段(time())
- PHP高效率写法
- XML需要注意的问题
- PHP 对 png 图像进行缩放,支持透明背景
- PHP登录验证码的实现与使用方法
- PHP常见的6个错误提示及解决方法
- Laravel事件广播
- php大转盘抽奖
- thinkphp ventor引入第三方类库 class not found