码迷,mamicode.com
首页 > Web开发 > 详细

yii2图片上传

时间:2015-09-24 14:37:35      阅读:344      评论:0      收藏:0      [点我收藏+]

标签:

yii2利用自带UploadedFile上传图片

public static function uploadFile($name)
    {
        $uploadedFile = UploadedFile::getInstanceByName($name);        
        if($uploadedFile === null || $uploadedFile->hasError)
        {
            return null;
        }        
        $ymd = date("Ymd");        
        $save_path = dirname(Yii::$app->basePath).‘\\web\\upload\\images\\‘. $ymd . "\\";
        $save_url = dirname(Yii::$app->homeUrl).‘/upload/images/‘ . $ymd . "/";        
        if(! file_exists($save_path))
        {
            mkdir($save_path);
        }       
        $file_name = $uploadedFile->getBaseName();
        $file_ext = $uploadedFile->getExtension();        
        // 新文件名
        $new_file_name = date("YmdHis") . ‘_‘ . rand(10000, 99999) . ‘.‘ . $file_ext;        
        $uploadedFile->saveAs($save_path . $new_file_name);       
        return [‘path‘ => $save_path, ‘url‘ => $save_url, ‘name‘ => $file_name, ‘new_name‘ => $new_file_name, ‘ext‘ => $file_ext];
    }

 

yii2图片上传

标签:

原文地址:http://www.cnblogs.com/baby123/p/4835089.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!