七牛图片上传

准备工作:

1)http://developer.qiniu.com/article/kodo/kodo-first/quickstart.html    跟着文档创建七牛帐号,进入个人中心申请认证(需要3天审核,一般1天就可以了)和创建存储空间(上传的图片都会在这个空间的内容管理显示)

2)http://developer.qiniu.com/code/v7/sdk/php.html   根据里面的过程跟着执行,用composer安装

 

 3)安装好以后,用如下代码上传图片:

控制器: 

use app\models\Ceshi;
use yii;
//导入七牛相关类
use Qiniu\Auth;
use Qiniu\Storage\BucketManager;
use Qiniu\Storage\UploadManager;

class TestController extends Controller {
    public function actionIndex() {
        $model = new Ceshi();
        $data = $model->find()->orderBy('id desc')->asArray()->all();
        if (Yii::$app->request->isPost) {
            $post = Yii::$app->request->post();
            //p($post);
            $image = $this->upload();
            if (!$image) {
                $model->addError('cover', '封面不能为空');
            } else {
                $post['Ceshi']['image'] = $image;
            }
            //p($post);
            if ($image) {
                $model->name = $post['Ceshi']['name'];
                $model->image = $post['Ceshi']['image'];
                if($model->save()){
                    p('添加成功');
                }
                p('添加成功11');
                Yii::$app->session->setFlash('info', '添加成功');
            } else {
                p('添加失败');
                Yii::$app->session->setFlash('info', '添加失败');
            }

        }
        //debug ( 'aaa' );
        return $this->render ( 'index',['model'=>$model,'data'=>$data]);
    }

    private function upload()
    {
        if ($_FILES['Ceshi']['error']['image'] > 0) {
            return false;
        }
        $token=$this->getToken();
        $uploadManager=new UploadManager();
        $key = uniqid();
        //$name=$_FILES['Ceshi']['name']['image'];
        $filePath=$_FILES['Ceshi']['tmp_name']['image'];
        $type=$_FILES['Ceshi']['type']['image'];
        list($ret,$err)=$uploadManager->putFile($token,$key,$filePath,null,$type,false);
        if($err){//上传失败
            returnJson("0001",[],"false",$err);
        }else{//成功
            //添加信息到数据库
            return $ret['key'];
        }
    }

    /**
     * 生成上传凭证
     * @return string
     */
    private function getToken(){
        $accessKey = Yii::$app->params['qiniu']['ak'];
        $secretKey = Yii::$app->params['qiniu']['sk'];
        $auth=new Auth($accessKey, $secretKey);
        $bucket = Yii::$app->params['qiniu']['bucket'];//上传空间名称
        //设置put policy的其他参数
        //$opts=['callbackUrl'=>'http://www.callback.com/','callbackBody'=>'name=$(fname)&hash=$(etag)','returnUrl'=>"http://www.baidu.com"];
        return $auth->uploadToken($bucket);//生成token
    }
}

视图:

<?php
use yii\bootstrap\ActiveForm;
use yii\helpers\Html;
?>


<?php

$form = ActiveForm::begin([
    'options' => [
        'enctype' => 'multipart/form-data',
    ],
    //'action' => ['test/ceshi'],
]);
?>
<?=$form->field($model, 'name')->textInput()->label('名称');?>
<?=$form->field($model, 'image')->fileInput()->label('图片');?>
<?php echo Html::submitButton('提交', ['class' => 'btn-glow primary']); ?>

<?php ActiveForm::end(); ?>

<br/>
<ul>
    <?php foreach($data as $v){ ?>
    <li><?=$v['id']?></li>
    <li><?=$v['name']?></li>
    <li><?=$v['image']?></li>
    <?php } ?>
</ul>
View Code

 

posted @ 2017-01-19 18:04  星希望  阅读(1402)  评论(1编辑  收藏  举报