zoukankan      html  css  js  c++  java
  • 七牛图片上传

    准备工作:

    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 appmodelsCeshi;
    use yii;
    //导入七牛相关类
    use QiniuAuth;
    use QiniuStorageBucketManager;
    use QiniuStorageUploadManager;
    
    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 yiiootstrapActiveForm;
    use yiihelpersHtml;
    ?>
    
    
    <?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
  • 相关阅读:
    Django-4
    Django-3
    博客中涉及的源码下载
    源码阅读系列:EventBus
    使用DFA做文本编辑器的自动提示
    计算机网络-IP类型判断
    感兴趣的文章搜集
    The Engine Document of JustWeEngine
    [译文]选择使用正确的 Markdown Parser
    Android源码阅读-Filter过滤器
  • 原文地址:https://www.cnblogs.com/herry52/p/6307665.html
Copyright © 2011-2022 走看看