【发布时间】: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