duanli0162 2014-11-11 11:09
浏览 170

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条回答 默认 最新

  • douyunhuan9886 2014-11-16 00:54
    关注

    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...**

    评论

报告相同问题?

悬赏问题

  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程