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

    评论

报告相同问题?

悬赏问题

  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了