Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 文件上传(Yii)_Php_Upload_Yii - Fatal编程技术网

Php 文件上传(Yii)

Php 文件上传(Yii),php,upload,yii,Php,Upload,Yii,我正在实现一些文件上传字段,我遇到了一些困难。 我一直遵循这一点,一切都很完美,直到我尝试更新我的模型。 如果我没有填写文件字段,则在保存后会将其清除。我在谷歌上搜索找到了这个。我已经修改了代码,但是现在,当我试图修改另一个字段时,它不会保存,除非我也修改了图像字段。 问题出在哪里? 我的型号代码: public function actionUpdate($id) { $model=$this->loadModel($id); // Un

我正在实现一些文件上传字段,我遇到了一些困难。 我一直遵循这一点,一切都很完美,直到我尝试更新我的模型。 如果我没有填写文件字段,则在保存后会将其清除。我在谷歌上搜索找到了这个。我已经修改了代码,但是现在,当我试图修改另一个字段时,它不会保存,除非我也修改了图像字段。 问题出在哪里? 我的型号代码:

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

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if(isset($_POST['Company']))
        {
            $model->attributes=$_POST['Company'];
            $the_image = CUploadedFile::getInstance($model,'image');
            if (is_object($the_image) && get_class($the_image)==='CUploadedFile')
              $model->image = $the_image;
            if($model->save())
                if (is_object($the_image))
                $model->image->saveAs(Yii::getPathOfAlias('webroot') . '/upload/' . $model->image);
                $this->redirect(array('view','id'=>$model->id));
        }

        $this->render('update',array(
            'model'=>$model,
    }
以下是我的模型中的规则:

            array('name', 'required'),
            array('type, number, rating, user_id', 'numerical', 'integerOnly'=>true),
            array('name, image, address, site, mail', 'length', 'max'=>1000),
            array('text', 'safe'),
            array('image', 'file', 'types'=>'jpg, gif, png'),
            array('image', 'unsafe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, type, name, text, address, site, mail, number, rating, user_id', 'safe', 'on'=>'search'),

请帮帮我。我不知道它为什么不起作用。

尝试在更新中修改
图像
属性规则,如下所示:

array('image','file','types'=>'jpg,gif,png','allowEmpty'=>true','on'=>'update'),

我找到了这个技巧,希望它对你有用。

尝试在更新中修改你的
图像
属性规则,如下所示:

public function uploadMultifile ($model,$attr,$path)
        {
            if($sfile=CUploadedFile::getInstances($model, $attr)){
              foreach ($sfile as $i=>$file){  
                 $formatName=time().$i.'.'.$file->getExtensionName();
                 $file->saveAs(Yii::app()->basePath  .DIRECTORY_SEPARATOR.'..'. $path.$formatName);
                     //  $model->image->saveAs(Yii::app()->basePath . '/../studentimage/' . $date . $model->image);
                 $ffile[$i]=$formatName;
                 }
                return ($ffile);
             }
         }

public function actionCreate()
{
    $model=new Subject;
    // $model->date_erected = date('Y-m-d');
    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

    if(isset($_POST['Subject']))
    {
        $model->attributes=$_POST['Subject'];
                    if($filez=$this->uploadMultifile($model,'imagea','/../images/views/'))
                {
                    $model->documents=implode(",", $filez);
                }
        if($model->save())
            $this->redirect(array('view','id'=>$model->id));
    }

    $this->render('create',array(
        'model'=>$model,
    ));
}
array('image','file','types'=>'jpg,gif,png','allowEmpty'=>true','on'=>'update'),

我找到了这个窍门,希望对你有用。

非常感谢。你救了我的命!:)非常感谢你。你救了我的命!:)在回答问题时,如果你大声说出发生了什么变化就好了。在回答问题时,如果你大声说出发生了什么变化就好了。
public function uploadMultifile ($model,$attr,$path)
        {
            if($sfile=CUploadedFile::getInstances($model, $attr)){
              foreach ($sfile as $i=>$file){  
                 $formatName=time().$i.'.'.$file->getExtensionName();
                 $file->saveAs(Yii::app()->basePath  .DIRECTORY_SEPARATOR.'..'. $path.$formatName);
                     //  $model->image->saveAs(Yii::app()->basePath . '/../studentimage/' . $date . $model->image);
                 $ffile[$i]=$formatName;
                 }
                return ($ffile);
             }
         }

public function actionCreate()
{
    $model=new Subject;
    // $model->date_erected = date('Y-m-d');
    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

    if(isset($_POST['Subject']))
    {
        $model->attributes=$_POST['Subject'];
                    if($filez=$this->uploadMultifile($model,'imagea','/../images/views/'))
                {
                    $model->documents=implode(",", $filez);
                }
        if($model->save())
            $this->redirect(array('view','id'=>$model->id));
    }

    $this->render('create',array(
        'model'=>$model,
    ));
}