pdf文件没有上传到yii框架中的服务器

时间:2013-02-22 06:23:17

标签: php yii yii-extensions

您好我在这里使用CActiveFrom fileField上传服务器中的文件,但pdf文件没有保存剩下的所有扩展名都保存在这里是我的控制器代码来保存文件

控制器

 if(isset($_POST['Uploadinfo']))
        {
            $model->attributes=$_POST['Uploadinfo'];


            $file = CUploadedFile::getInstance($model, 'imageId');


            $model->imageId=CUploadedFile::getInstance($model, 'imageId');


            $valid = $model->validate();


            date_default_timezone_set('Asia/Kolkata');
            $serverTime = date("Y-m-d H:i:s", time());

            if(!empty($file->name) && $valid)
            {

                $modelFileLocation->filename = microtime() . $file->name;
                $modelFileLocation->orgfilename = $file->name;
                $modelFileLocation->type = 1;
                $modelFileLocation->createdOn = $serverTime;
                $modelFileLocation->lastModifiedBy = 'lecturer';

                $valid = $modelFileLocation -> validate();


                $modelFileLocation->deptId = $_POST['Uploadinfo']['departmentId'];
                print_r($file);

                if ($modelFileLocation->save())
                {
                    $file = CUploadedFile::getInstance($model, 'imageId');
                    print_r($modelFileLocation);
                print_r($file);
                die();  

                $model->userId = $_POST['Uploadinfo']['userId'];
                $model->imageId = $modelFileLocation->id;
                $model->departmentId = 1;
                $model->createdOn = $serverTime;
                $model->lastModifiedBy = 'lecturer';

                    if ($model->save())
                    {

                        $modeldocumentinfo->property = $file->type;
                        $modeldocumentinfo->fileName = $modelFileLocation->filename;
                        $modeldocumentinfo->creator = 'lecturer';
                        $modeldocumentinfo->lastUploadedOn = $serverTime;
                        $modeldocumentinfo->uploadId = $model->id;
                        $modeldocumentinfo->viewers = 0;

                        $modeldocumentinfo->save();

                        $file->saveAs('uploads/files/' . $modelFileLocation->filename);
                            print_r($file);
            die();
                        //Yii::app()->user->setFlash('uploadsuccess', "File Uploaded Successfully...");
                        $this->redirect(array('lmaterialitview'));
                    }
                }

在上面的代码中我打印$ modelFileLocation模型之前和之后的文件实例,在此模型保存文件实例之前保存但是在保存模型后文件实例变空

这是实例代码

实例消息

 CUploadedFile Object ( [_name:CUploadedFile:private] => example.pdf [_tempName:CUploadedFile:private] => C:\wamp\tmp\php4BB.tmp [_type:CUploadedFile:private] => application/pdf [_size:CUploadedFile:private] => 205374 [_error:CUploadedFile:private] => 0 [_e:CComponent:private] => [_m:CComponent:private] => )

并且在第二个print_r()中它消失了但是对于剩余的扩展它会出现并保存

1 个答案:

答案 0 :(得分:0)

只需将$ deleteTempFile参数设置为false

即可
$file->saveAs('uploads/files/' . $modelFileLocation->filename, false);