Yii Framework将提交的文件存储/保存到images文件夹


Yii Framework store/save submitted file to images folder

嗨,在过去的两天里,我阅读了很多关于在Yii中将文件保存到文件夹的教程,但到目前为止,它们都不起作用。我有以下表格:

<div class="form">
<?php $form = $this->beginWidget('CActiveForm', array(
'htmlOptions' => array('enctype' => 'multipart/form-data')
)); ?>
<?php echo $form->errorSummary($model); ?>
<div class="row">
    <?php echo $form->labelEx($model,'Binaryfile'); ?>
    <?php echo $form->fileField($model,'uploadedFile'); ?>
    <?php echo $form->error($model,'uploadedFile'); ?>
</div>
<div class="row buttons">
    <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
endWidget();?>

file字段将代码提交给mysql数据库中的BLOB字段。控制器如下:

public function actionCreate()
{
    $model=new Estudos;
    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);
    if(isset($_POST['Estudos']))
    {
        $model->attributes=$_POST['Estudos'];
        $model->binaryfile = CUploadedFile::getInstance($model,'binaryfile'); // grava na bd no campo binaryfile
        // $model->binaryfile->saveAs(Yii::app()->params['uploadPath']);
        if($model->save())
            $this->redirect(array('view','id'=>$model->id));
    }
    $this->render('create',array(
        'model'=>$model,
    ));
}

模型是这样的:

public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array('fileName', 'length', 'max'=>100),
        array('fileType', 'length', 'max'=>50),
        array('binaryfile', 'safe'),
        // The following rule is used by search().
        // @todo Please remove those attributes that should not be searched.
        array('id, fileName, fileType, binaryfile', 'safe', 'on'=>'search'),
    );
}
public $uploadedFile;
// Gravar imagem na base de dados - cria blob field
public function beforeSave()
{
    if ($file = CUploadedFile::getInstance($this, 'uploadedFile'))
    {
        $this->fileName = $file->name;
        $this->fileType = $file->type;
        $this->binaryfile = file_get_contents($file->tempName);
    }
    return parent::beforeSave();
}

代码可以将文件存储为BLOB字段,但我需要更改代码以将文件存储在images文件夹中,然后显示允许在任何浏览器中打开文件(pdf文件)的链接。为了将文件存储在images文件夹中,我尝试在控制器actionCreate中保存As(),但Yii冻结了,网页变为空白,没有任何错误,只是空白

**任何人都可以帮助我…我非常非常需要这个。非常感谢。**

终于我自己想好了。答案很简单,但我花了4天时间才写完。在我的actionCreate()中,我做了:

public function actionCreate()
{
    $model=new Estudos;

    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);
    if(isset($_POST['Estudos']))
    {
        $model->attributes=$_POST['Estudos'];
        $model->uploadedFile=CUploadedFile::getInstance($model,'uploadedFile');
        if($model->save())
            $model->uploadedFile->saveAs("pdfs/".$model->uploadedFile,true);
            $this->redirect(array('view','id'=>$model->id));    
    }
    $this->render('create',array(
        'model'=>$model,
    ));
}

**通过这种方式,saveAs()函数就像一个魅力,现在将我提交的文件保存在pdfs文件夹中。下一步是尝试并弄清楚如何为提交到pdfs文件夹的所有文件创建链接。也许使用foreach()循环。

致以最良好的问候…**