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

Hi, for the past 2 days i've been reading and reading lots of tutorials about saving files to folders in Yii, and neither of them have worked so far. I have the folowing form:

<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(); ?>

The file field submits the code to a BLOB field in mysql database. The Controller is as follows:

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,
    ));
}

And the Model is this one:

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();
}

The code works fine to store a file as a BLOB field, but i need to change the code to store the file in images folder and next to display links that permits to open the file (pdf file) in any browser. To store the file in images folder i tryed saveAs() in my controller actionCreate but Yii freezes and the webpage becames blank with no error, just blank.

**Anyone can help me... I need this very very much. Many thanks in advance. **

2个回答



检查此链接,在此链接中说明如何...
http://www.yiiframework.com/wiki/2/how-to-upload -a-file-using-a-model / </ p>
</ div>

展开原文

原文

check this link,,in this link say how do it... http://www.yiiframework.com/wiki/2/how-to-upload-a-file-using-a-model/



最后我自己想通了。 答案很简单,但花了我4天的时间来写它。
在我的actionCreate()中我做了:</ strong> </ p>

  public function actionCreate()
{

$ model = new Estudos;

//如果需要AJAX验证,请取消注释以下行
// $ this-&gt; performAjaxValidation($ model);

if(isset($ _ POST) ['Estudos']))
{
$ model-&gt; attributes = $ _ POST ['Estudos'];
$ model-&gt; uploadedFile = CUploadedFile :: getInstance($ model,'uploadedFile'); \ n if($ model-&gt; save())
$ model-&gt; uploadedFile-&gt; saveAs(“pdfs /".$ model-&gt; uploadedFile,true);
$ this-&gt; redirect(array) ( '观看', 'ID'=&GT; $模型 - &GT; ID));

}

$ this-&gt; render('create',array(
'model'=&gt; $ model,
));
}
</ code> </ pre> \ n

**这样,saveAs()函数像魅力一样工作,现在将我的提交文件保存在pdfs文件夹中。
下一步是尝试找出如何为提交给pdfs的所有文件创建链接 文件夹。
可以使用foreach()循环。</ p>

祝你好运...... ** </ p>
</ div>

展开原文

原文

Finally i've figured it by myself. The answer was rather simple, but took me 4 days to write it. In my actionCreate() i did:

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,
    ));
}

**That way the saveAs() function worked like a charm and now saves my submited files in the pdfs folder. The next step is to try and figure out how to create links for all files submitted to pdfs folder. Maybe with a foreach() loop.

Best regards...**

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐