谁一开始就是大神?

PHP 第44页

PHP

为什么response的headers属性不能修改Content-Type,想输出图片[2.0]

Cleverelephant阅读(48)

但是它永远是 text/html; charset=UTF-8

输出图片,可以这样:
当然路径存在之类的判断你自己要做好,如果确定图片格式是jpeg,那么可以写死mime
应该是可以的吧
如果最后是通过yii框架默认的return渲染视图输出的,就是html头,因为header会覆盖。你尝试不用return,用echo
浏览器在跨域的时候会先发出一个preflight,如果改了header。然后服务器不允许改content-type
//统一response返回json格式
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
我是想统一返回json的,百度到你的问题,我这边解决了,顺便给你一些建议

yii2.0basic图片上传不了问题[2.0]

lovelybubble阅读(53)

我的视图:

/view/ext/_form.php
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $model app\models\Ext */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="ext-form">

    <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?> 

    <?= $form->field($model, 'image1')->fileInput() ?>

    <?= $form->field($model, 'image2')->fileInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'info')->textInput(['maxlength' => true]) ?>

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? '创建' : '更新', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>
    <?php ActiveForm::end(); ?>
</div>

这是model
models/ext.php

<?php
namespace app\models;
use Yii;
use yii\web\UploadedFile;
/**
 * This is the model class for table "ext".
 *
 * @property integer $id
 * @property string $image1
 * @property string $image2
 * @property string $info
 */
class Ext extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'ext';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['image1', 'image2'], 'string', 'max' => 60],
            [['image1', 'image2'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg'],
            [['info'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'image1' => 'logo图片',
            'image2' => '微信二维码图片',
            'info' => '站点简介',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function upload()
    {        
        if ($this->validate()) 
        {
            $randName=date("Y").date("m").date("d").date("H").date("i").date("s").rand(100, 999).'-'.md5(microtime());   //生成随机文件名
            $uploadPath= 'uploads/' . $randName . '.' . $this->image1->extension;  //设置保存路径, 为 backend\web\uploads
            $this->image1->saveAs($uploadPath);  //保存文件
            return Url::to('app/web/'.$uploadPath,true); //返回文件的 url 路径,使用 Url 帮助类。
        } 
        else 
        {            
            return false;
        }
    }
}

控制器:
controllers/Extcontroller.php

<?php
namespace app\controllers;
use Yii;
use app\models\Ext;
use app\models\ExtSearch;
use yii\web\Controller;
use yii\web\UploadedFile;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

class ExtController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all Ext models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new ExtSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Ext model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        ......
    }

    /**

    public function actionCreate()
    {
        $model = new Ext();
        //$model->wb_logo = UploadedFile::getInstance($model, 'image1');
        if ($model->load(Yii::$app->request->post()) ) {
            //var_dump(Yii::$app->request->post());die;
            $model->save();
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    protected function findModel($id)
    {
        if (($model = Ext::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    
    public function actionUpload()
    {
        $model = new Ext();

        if (Yii::$app->request->isPost) {
            $model->image1 = UploadedFile::getInstance($model, 'image1');
            if ($model->upload()) {
                // 文件上传成功
                return;
            }
        }
        return $this->render('upload', ['model' => $model]);
    }
}

现在就是一直成功不了

[[‘image1’, ‘image2’], ‘string’, ‘max’ => 60],
[[‘image1’, ‘image2’], ‘string’, ‘max’ => 60],

yii2公共变量在哪里设置?[2.0]

Lvysea阅读(55)

yii2高级版公共变量在哪里设置?

方式挺多的,看你习惯吧,你可以在params.php或者params-local.php配置文件中,注意当然如果在这配置文件中配置,就得注意它的加载顺序,yii2按以下的顺序读取配置:
1、common/config/params.php
2、common/config/params-local.php
3、frontend/config/params.php
4、frontend/config/params-local.php
所以,如果你的公共变量前后台都公用,那你可以配置到common/config/params.php或common/config/params-local.php,如果是前台用的到那配置到frontend/config/params.php或frontend/config/params-local.php,后台用的到那配置到backend/config/params.php或backend/config/params-local.php中。
读取则是: echo Yii::$app->params[‘adminEmail’]; 表示读取配置文件中配置的adminEmail这个变量,按照上面的顺序读取,后面的配置文件会覆盖掉前面的配置文件定义的值。如果所有配置都读取不到,返回null。
还有,我个人的习惯是自己定义个常量类,然后定义静态的属性,使用的时候通过 “类::$属性” 来读取。
怎么用还是很灵活的,没有特别的要求。就看你的使用习惯

GridView::widget显示出来的图片太大[2.0]

FionaCherry阅读(49)

[
    'attribute'=>'img',
    'format' => ['raw',],
    'value' => function($model){
        return Html::img($model->img);
    }
],

我想要让它变小一点,就修改成了

[
    'attribute'=>'img',
    'format' =>[
        'image',
        [
            'height' =>50,
            'width' => 100
        ]
    ],
    'value' => function($model) {
        return Html::img($model->img);
    }
],

可是图片不显示了,怎么回事?

参数不正确

yii2.0图片上传[2.0]

lionsuper阅读(56)

5.png
“微博图片”和“微信二维码”要做成选择图片上传的那种怎么实现啊?

图片上传,一般数据库直接存放图片的地址。yii2中,可以利用一些封装好的组件对图片进行处理。参考:http://t.cn/RVOUrZm

sublime显示缩进线

LvyJames阅读(56)

sublime 缩进线
在哪里设置可以显示出来。

打开 Preferences -> Settings-Default
无标题.png
在里面搜索indent_guide该关键字,会找到两个结果
1.png
此时你如果设置成和我上面截图一样的的话,就会呈现下面的结果
2.png
你会看到两个红框中的白线一个“深”,一个“浅”,“深”是由于我的鼠标放在了那个函数上,这个效果是由
“indent_guide_options”: [“draw_normal”,”draw_active”],中的”draw_active”发挥的作用,如果你改为
“indent_guide_options”: [“draw_normal”],那么呈现出来的都是“浅”的。
好了,若果有其他不太懂的再交流吧!
是有的但不明顯, 想要明顯的話
第一 : Indent Guide Color
第二 : Hide tab lines
你要的是这种效果吗?捕获.PNG
有没有人遇到过 ubuntu 环境线的sb 缩进线出问题的情况。
目前是这样的 2017-03-06 16:01:57屏幕截图.png
这个线没有跟我设置的缩进tab(4个空格)匹配,导致代码折叠出问题了

求助,yii2加载ckeditor抛出InvalidParameter异常。[2.0]

dmCherry阅读(58)

在加载kartik\detail\DetailView时,开启ckeditor后,出现下面异常:

Invalid Parameter – yii\base\InvalidParamException
Trying to copy a directory to itself or a subdirectory.

看了下,问题可能出在下面的第2条:

BaseFileHelper::copyDirectory('/Users/qijiaona/var/www/intel/backend/web/assets', '/Users/qijiaona/var/www/intel/backend/web/assets/da31e3d5');

关闭ckeditor插件后就可以正常运行了。
求解决。

1. in /Users/qijiaona/var/www/intel/vendor/yiisoft/yii2/helpers/BaseFileHelper.php at line 260
2. in /Users/qijiaona/var/www/intel/vendor/yiisoft/yii2/web/AssetManager.php at line 542 – yii\helpers\BaseFileHelper::copyDirectory('/Users/qijiaona/var/www/intel/ba...', '/Users/qijiaona/var/www/intel/ba...', ['dirMode' => 509, 'fileMode' => null, 'beforeCopy' => Closure])
3. in /Users/qijiaona/var/www/intel/vendor/yiisoft/yii2/web/AssetManager.php at line 458 – yii\web\AssetManager::publishDirectory('/Users/qijiaona/var/www/intel/ba...', [])
4. in /Users/qijiaona/var/www/intel/vendor/yiisoft/yii2/web/AssetBundle.php at line 183 – yii\web\AssetManager::publish('assets', [])
5. in /Users/qijiaona/var/www/intel/vendor/yiisoft/yii2/web/AssetManager.php at line 266 – yii\web\AssetBundle::publish(yii\web\AssetManager)
6. in /Users/qijiaona/var/www/intel/vendor/yiisoft/yii2/web/AssetManager.php at line 237 – yii\web\AssetManager::loadBundle('dosamigos\ckeditor\CKEditorWidge...', [], true)
7. in /Users/qijiaona/var/www/intel/vendor/yiisoft/yii2/web/View.php at line 284 – yii\web\AssetManager::getBundle('dosamigos\ckeditor\CKEditorWidge...')
8. in /Users/qijiaona/var/www/intel/vendor/yiisoft/yii2/web/AssetBundle.php at line 123 – yii\web\View::registerAssetBundle('dosamigos\ckeditor\CKEditorWidge...')
9. in /Users/qijiaona/var/www/intel/vendor/2amigos/yii2-ckeditor-widget/src/CKEditor.php at line 57 – yii\web\AssetBundle::register(yii\web\View)
10. in /Users/qijiaona/var/www/intel/vendor/2amigos/yii2-ckeditor-widget/src/CKEditor.php at line 44 – dosamigos\ckeditor\CKEditor::registerPlugin()
11. in /Users/qijiaona/var/www/intel/vendor/yiisoft/yii2/base/Widget.php at line 102 – dosamigos\ckeditor\CKEditor::run()
12. in /Users/qijiaona/var/www/intel/vendor/yiisoft/yii2/widgets/ActiveField.php at line 703 – yii\base\Widget::widget(['preset' => 'full', 'clientOptions' => ['allowedContent' => true, 'extraPlugins' => 'lineheight,uploadimage', 'line_height' => '1;1.2;1.5;2;2.5;3', 'imageUploadUrl' => '/accelerator/upload'], 'model' => common\models\Accelerator, 'attribute' => 'acc_desc_long', ...])
13. in /Users/qijiaona/var/www/intel/vendor/kartik-v/yii2-detail-view/DetailView.php at line 873 – yii\widgets\ActiveField::widget('\dosamigos\ckeditor\CKEditor', ['preset' => 'full', 'clientOptions' => ['allowedContent' => true, 'extraPlugins' => 'lineheight,uploadimage', 'line_height' => '1;1.2;1.5;2;2.5;3', 'imageUploadUrl' => '/accelerator/upload']])
14. in /Users/qijiaona/var/www/intel/vendor/kartik-v/yii2-detail-view/DetailView.php at line 785 – kartik\detail\DetailView::renderFormAttribute(['attribute' => 'acc_desc_long', 'format' => 'raw', 'type' => 'widget', 'widgetOptions' => ['class' => '\dosamigos\ckeditor\CKEditor', 'preset' => 'full', 'clientOptions' => ['allowedContent' => true, 'extraPlugins' => 'lineheight,uploadimage', 'line_height' => '1;1.2;1.5;2;2.5;3', 'imageUploadUrl' => '/accelerator/upload']], ...])
15. in /Users/qijiaona/var/www/intel/vendor/kartik-v/yii2-detail-view/DetailView.php at line 745 – kartik\detail\DetailView::renderAttributeItem(['attribute' => 'acc_desc_long', 'format' => 'raw', 'type' => 'widget', 'widgetOptions' => ['class' => '\dosamigos\ckeditor\CKEditor', 'preset' => 'full', 'clientOptions' => ['allowedContent' => true, 'extraPlugins' => 'lineheight,uploadimage', 'line_height' => '1;1.2;1.5;2;2.5;3', 'imageUploadUrl' => '/accelerator/upload']], ...])
16. in /Users/qijiaona/var/www/intel/vendor/kartik-v/yii2-detail-view/DetailView.php at line 718 – kartik\detail\DetailView::renderAttributeRow(['attribute' => 'acc_desc_long', 'format' => 'raw', 'type' => 'widget', 'widgetOptions' => ['class' => '\dosamigos\ckeditor\CKEditor', 'preset' => 'full', 'clientOptions' => ['allowedContent' => true, 'extraPlugins' => 'lineheight,uploadimage', 'line_height' => '1;1.2;1.5;2;2.5;3', 'imageUploadUrl' => '/accelerator/upload']], ...])
17. in /Users/qijiaona/var/www/intel/vendor/kartik-v/yii2-detail-view/DetailView.php at line 611 – kartik\detail\DetailView::renderDetailView()
18. in /Users/qijiaona/var/www/intel/vendor/kartik-v/yii2-detail-view/DetailView.php at line 548 – kartik\detail\DetailView::runWidget()
19. in /Users/qijiaona/var/www/intel/vendor/yiisoft/yii2/base/Widget.php at line 102 – kartik\detail\DetailView::run()
20. in /Users/qijiaona/var/www/intel/backend/views/accelerator/view.php at line 176 – yii\base\Widget::widget(['model' => common\models\Accelerator, 'mode' => 'view', 'formOptions' => ['options' => ['enctype' => 'multipart/form-data']], 'panel' => ['heading' => '众创空间详情', 'type' => 'primary'], ...])
21. in /Users/qijiaona/var/www/intel/vendor/yiisoft/yii2/base/View.php at line 326require('/Users/qijiaona/var/www/intel/ba...')
22. in /Users/qijiaona/var/www/intel/vendor/yiisoft/yii2/base/View.php at line 248 – yii\base\View::renderPhpFile('/Users/qijiaona/var/www/intel/ba...', ['model' => common\models\Accelerator, 'dataProvider' => yii\data\ActiveDataProvider, 'dpMentor' => yii\data\ActiveDataProvider, 'dpInvestor' => yii\data\ActiveDataProvider, ...])
23. in /Users/qijiaona/var/www/intel/vendor/yiisoft/yii2/base/View.php at line 150 – yii\base\View::renderFile('/Users/qijiaona/var/www/intel/ba...', ['model' => common\models\Accelerator, 'dataProvider' => yii\data\ActiveDataProvider, 'dpMentor' => yii\data\ActiveDataProvider, 'dpInvestor' => yii\data\ActiveDataProvider, ...], backend\controllers\AcceleratorController)
24. in /Users/qijiaona/var/www/intel/vendor/yiisoft/yii2/base/Controller.php at line 378 – yii\base\View::render('view', ['model' => common\models\Accelerator, 'dataProvider' => yii\data\ActiveDataProvider, 'dpMentor' => yii\data\ActiveDataProvider, 'dpInvestor' => yii\data\ActiveDataProvider, ...], backend\controllers\AcceleratorController)
25. in /Users/qijiaona/var/www/intel/backend/controllers/AcceleratorController.php at line 189 – yii\base\Controller::render('view', ['model' => common\models\Accelerator, 'dataProvider' => yii\data\ActiveDataProvider, 'dpMentor' => yii\data\ActiveDataProvider, 'dpInvestor' => yii\data\ActiveDataProvider, ...])
26. backend\controllers\AcceleratorController::actionView('58')
27. in /Users/qijiaona/var/www/intel/vendor/yiisoft/yii2/base/InlineAction.php at line 55 – call_user_func_array([backend\controllers\AcceleratorController, 'actionView'], ['58'])
28. in /Users/qijiaona/var/www/intel/vendor/yiisoft/yii2/base/Controller.php at line 154 – yii\base\InlineAction::runWithParams(['r' => 'accelerator/view', 'id' => '58'])
29. in /Users/qijiaona/var/www/intel/vendor/yiisoft/yii2/base/Module.php at line 454 – yii\base\Controller::runAction('view', ['r' => 'accelerator/view', 'id' => '58'])
30. in /Users/qijiaona/var/www/intel/vendor/yiisoft/yii2/web/Application.php at line 100 – yii\base\Module::runAction('accelerator/view', ['r' => 'accelerator/view', 'id' => '58'])
31. in /Users/qijiaona/var/www/intel/vendor/yiisoft/yii2/base/Application.php at line 375 – yii\web\Application::handleRequest(yii\web\Request)
32. in /Users/qijiaona/var/www/intel/backend/web/index.php at line 18 – yii\base\Application::run()

ps: 我看到之前有人也遇到过类似的问题,比如sgmengfault上的这个yii2 Trying to copy a directory to itself or a subdirectory,还有yiichina上的这个创建表单报错 ,但这两个帖子都与我遇到的情况不同,求解决。

我的AppAsset类配置

    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [
        'css/site.css',
    ];
    public $js = [
    ];
    public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap\BootstrapAsset',
    ];

应该改成这样:
修改yii2-ckeditor-widget文件夹下面的CKEditorWidgetAsset.php文件
修改

Yii2怎么执行原生SQL语句(添加数据)[2.0]

JadeLion阅读(49)

数据是一个大量的数组,我需要拼接 SQL 一起添加到数据库

INSERT INTO `erm_course` ('course_name','course_type_id','course_time') VALUES ('1','1','5'), ('1','1','7'), ('1','1','7'), ('1','1','7'), ('1','1','7');

要执行这条 SQL 语句

生成.csv的代码

Hazelyak阅读(49)

看到别人写了段生成.csv的代码中有这么一句 $filename = fopen(‘php://output’, ‘w’);fputcsv($file, array(‘All’,’–‘,’–‘,111)); 不明白php://output是什么意思?

php://output 是一个只写的数据流, 允许你以 print 和 echo 一样的方式 写入到输出缓冲区。官网手册
fopen的第一个参数应该是要打开的文件名或者url

登录

找回密码

注册