【问题标题】:Yii Framework store/save submitted file to images folderYii 框架存储/保存提交的文件到图像文件夹
【发布时间】:2014-11-11 11:09:49
【问题描述】:

嗨,在过去的两天里,我一直在阅读和阅读很多关于在 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 字段,但我需要更改代码以将文件存储在图像文件夹中,并在旁边显示允许以任何方式打开文件(pdf 文件)的链接浏览器。 要将文件存储在图像文件夹中,我在控制器 actionCreate 中尝试了 saveAs(),但 Yii 冻结,网页变为空白,没有错误,只是空白。

**任何人都可以帮助我...我非常需要这个。提前谢谢了。 **

【问题讨论】:

    标签: php mysql yii frameworks


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      终于我自己想通了。答案很简单,但我花了 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() 循环。

      最好的问候...**

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-01-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-17
        • 1970-01-01
        相关资源
        最近更新 更多