Home > php教程 > php手册 > yii上传文件或图片实例

yii上传文件或图片实例

WBOY
Release: 2016-06-13 09:39:53
Original
1481 people have browsed it

1.test.php

复制代码 代码如下:









‘/test/upload/'改成你相应的地址。
2.TestController.php

复制代码 代码如下:


    public function actionUpload()
    {

        $dir = isset($_REQUEST['dir']) ? $_REQUEST['dir'] : '';
        $image = CUploadedFile::getInstanceByName('file');
        $dir=Yii::getPathOfAlias('webroot').'/images/'.$dir.'/';
        //上传目录
        if (!is_dir($dir)) {
            mkdir($dir);
        //目录不存在则创建
        }
        $name = $dir.$image->name;
        //文件名绝对路径

        $status = $image->saveAs($name,true);
        //保存文件
        if ($status) {
            echo 'success';
        }else {
            echo 'fail';
        }
    }


3. 测试

访问http://www.ttlsa.com/test/test,上传你的文件即可。如果出现succes说明成功了。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template