【发布时间】:2014-03-03 11:59:47
【问题描述】:
请帮帮我。
在 yii 中更新/编辑时获取上传的图片
在 CRUD 操作中
当我在创建和删除时在 cmultifile 中上传文件时工作正常,但我的问题是在更新该特定表单期间。
如果有任何建议保留 Cmultifile 中的下拉字段和文件,请始终根据数据库值进行设置。
请帮助我获取 CMultiFileUpload 小部件...从选定的数据库中获取文件
她的我的代码
在表格中 使用了这个小部件
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'upload-form',
// Please note: When you enable ajax validation, make sure the corresponding
// controller action is handling ajax validation correctly.
// There is a call to performAjaxValidation() commented in generated controller code.
// See class documentation of CActiveForm for details on this.
'enableAjaxValidation'=>false,
'htmlOptions' => array(
'enctype' => 'multipart/form-data',
),
)); ?>
<?php $this->widget('CMultiFileUpload',
array(
'model'=>$model,
'attribute' => 'Document',
'accept'=>'jpg|gif|png|doc|docx|pdf',
'denied'=>'Only doc,docx,pdf and txt are allowed',
'max'=>4,
'remove'=>'[x]',
'duplicate'=>'Already Selected',
)
);?>
在控制器中
public function actionCreate()
{
$model = new Upload;
echo Yii::app()->basePath.'/Images/';
if(isset($_POST['Upload']))
{
if($filez=$this->uploadMultifile($model,'Document','/Images/'))
{
$model->Document=implode(",", $filez);
}
$model->attributes=$_POST['Upload'];
if ($model->save())
{
$this->redirect(array('view', 'id' => $model->idUpload));
}
}
$this->render('create', array(
'model' => $model,
));
}
//Function for uploading and saving Multiple files
public function uploadMultifile ($model,$attr,$path)
{
/*
* path when uploads folder is on site root.
* $path='/uploads/doc/'
*/
if($sfile=CUploadedFile::getInstances($model, $attr)){
foreach ($sfile as $i=>$file){
// $formatName=time().$i.'.'.$file->getExtensionName();
$fileName = "{$sfile[$i]}";
$formatName=time().$i.'_'.$fileName;
$file->saveAs(Yii::app()->basePath.$path.$formatName);
$ffile[$i]=$formatName;
}
return ($ffile);
}
}
【问题讨论】:
-
耶?请给代码!向我们展示您的控制器、模型和视图
-
添加了表单和控制器的代码...请帮助我进行 CMultiFileUpload
-
“请帮助我获取 CMultiFileUpload 小部件...从选定的数据库中获取文件”:但我看不到任何“DB-Select-Request”?这个问题代表什么?您是否从这边复制了您的解决方案? stackoverflow.com/questions/21854460/…
-
是的,因为它是我的解决方案和问题...我的问题是在更新/编辑上传的文件时应该出现在为该模型选择的小部件上