使用Yii上传图片

时间:2015-06-04 08:28:56

标签: php yii

我在Yii1中遇到文件上传问题..

这是我的控制器

<?php

class SiteController extends Controller
{
    public function actionIndex()
    {
        $this->render('upload', array('model'=>new User));
    }

    public function actionSimpan() {
        $model = new User;
        //$direc = Yii::app()->basePath.'\images\\';
        if(isset($_POST)) {
        //if(Yii::app()->request->isPostRequest) {
            $random = rand(0, 999);
            $uploadedFile = CUploadedFile::getInstance($model, 'profile');
            $fileName = "{$random}-{$uploadedFile}";
            $model->profile = $fileName;
            //$model->profile=CUploadedFile::getInstanceByName('profile');
            //print_r($direc); die();
            if($model->save()) {
                $uploadedFile->saveAs(Yii::app()->basePath.'\images\\'.$model->profile.'.jpg');
                echo CJSON::encode(array('status'=>true, 'pesan'=>'Berhasil'));
            } else {
                echo CJSON::encode(array('status'=>false, 'pesan'=>'Gagal'));
                die(CVarDumper::dump($model->errors,10,true));
            }
        }
    }

}

和我的观点

<?php 
    $form=$this->beginWidget('CActiveForm', array(
        'id'=>'form-upload',
        'htmlOptions'=>array('enctype'=>'multipart/form-data', ),
    ));
?>

<div class="row">
    <?php echo $form->labelEx($model, 'profile'); ?>
    <?php echo $form->fileField($model, 'profile'); ?>
</div>

<div class="row">
    <?php echo CHtml::Button('Simpan', array('onclick'=>'simpan()')); ?>
</div>

<?php $this->endWidget(); ?>

<script type="text/javascript">
    function simpan() {
        var data = $('#form-upload').serialize();
        $.ajax({
            type:'POST',
            url:'<?php echo Yii::app()->createUrl("site/simpan"); ?>',
            data: data,
            success: function(data) {
                var obj = jQuery.parseJSON(data);
                if(obj.status==true) {
                    alert(obj.pesan);
                } else {
                    alert(obj.pesan);
                }
            }
        });
    }
</script>

我尝试保存数据时遇到错误。在firebug中输出此错误:

{"status":false,"pesan":"Gagal"}<code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #007700">array<br />(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'profile'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array<br />&nbsp;&nbsp;&nbsp;&nbsp;(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'0'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'Profile&nbsp;cannot&nbsp;be&nbsp;blank.'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'1'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'Profile&nbsp;cannot&nbsp;be&nbsp;blank.'<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">)<br />)</span>
</span>
</code>

SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 33 of the JSON data


...ction(b){if(a.JSON&&a.JSON.parse)return a.JSON.parse(b+"");var c,d=null,e=m.trim...

我在模型中做了其他规则:

array('profile', 'file','types'=>'jpg, gif, png'),

有人遇到过类似的问题吗?

1 个答案:

答案 0 :(得分:1)

die(CVarDumper::dump($model->errors,10,true));

我需要说更多吗?

无论如何,这里有一个完整的答案:

此:

{"status":false,"pesan":"Gagal"}<code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #007700">array<br />(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'profile'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array<br />&nbsp;&nbsp;&nbsp;&nbsp;(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'0'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'Profile&nbsp;cannot&nbsp;be&nbsp;blank.'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'1'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'Profile&nbsp;cannot&nbsp;be&nbsp;blank.'<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">)<br />)</span>

不是JSON。无论如何都不是有效的JSON 但是页面上的某些JavaScript会尝试将其解析为JSON,因此会失败 那么,所有HTML垃圾来自哪里,这会打破你的JSON?
我无法验证这条线路的真正功能,但我很确定它会通过一些语法高亮显示转储:

die(CVarDumper::dump($model->errors,10,true));

换句话说,删除它。