我正在上传一张图片并且有一个空白的帖子数据。请帮帮我!
public function actionUpdate()
{
if(isset($_GET['TemplateId']))
{
$TemplateId = $_GET['TemplateId'];
}else{
$this->setFlash('error', 'Undefined template!');
$this->render('update');die();
}
$model = Store::model()->findByPk($this->module->getStoreId());
//exit($model->image_logo2);
//$model = AccountTemplate::model()->findByAttributes(array('account' => $account, 'template' => $TemplateId));
if($TemplateId==1)
{
$last_image = $model->image_logo1;
}else{
$last_image = $model->image_logo2;
}
if(isset($_POST['Store']))
{
echo "<pre>";print_r($_POST['Store']);die();
if($TemplateId==1)
{
$uploader = CUploadedFile::getInstance($model,'image_logo1');
}else{
$uploader = \CUploadedFile::getInstance($model,'image_logo2');
}
$sourcePath = pathinfo($uploader->getName());
$fileName = uniqid().'.'.$sourcePath['extension'];
if($sourcePath['extension']!="jpg" && $sourcePath['extension']!="png")
{
$this->setFlash('error', 'Only jpg or png files!');
}else{
if($TemplateId==1)
{
$model->image_logo1 = $fileName;
echo "<pre>";print_r($model->image_logo1);die();
}else{
$model->image_logo2 = $fileName;
echo "<pre>";print_r($model->image_logo2);die();
}
if($model->save()) {
$uploader->saveAs('uploads/'.$fileName);
if(file_exists('uploads/'.$last_image))
{
//unlink('uploads/'.$last_image);
}
$this->setFlash('success', 'New logo upload success');
}else{
$this->setFlash('error', 'New logo not upload!');
}
}
}
$this->render('update', array('model'=>$model, 'TemplateId' => $TemplateId));
}
<?php
if(!isset($model))
{
}else{
$form = $this->beginWidget(
'CActiveForm',
array(
'id' => 'upload-form',
'enableAjaxValidation' => false,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
)
);
// ...<img src='/uploads/products/10e83ff7ae580b7e198a38cf9133ff32.jpg'>
// ...
// ...
if($TemplateId==1)
{
echo $form->labelEx($model, "Logo image");
echo $form->fileField($model, 'image_logo1');
echo $form->error($model, 'image_logo1');
}else{
echo $form->labelEx($model, "Logo image");
echo $form->fileField($model, 'image_logo2');
echo $form->error($model, 'image_logo2');
}
echo CHtml::button('Update', array('submit' => array("Template/update/?TemplateId=$TemplateId")));
// ...
$this->endWidget();
}
?>
我的看法
<?php
if(!isset($model))
{
}else{
$form = $this->beginWidget(
'CActiveForm',
array(
'id' => 'upload-form',
'enableAjaxValidation' => false,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
)
);
// ...<img src='/uploads/products/10e83ff7ae580b7e198a38cf9133ff32.jpg'>
// ...
// ...
if($TemplateId==1)
{
echo $form->labelEx($model, "Logo image");
echo $form->fileField($model, 'image_logo1');
echo $form->error($model, 'image_logo1');
}else{
echo $form->labelEx($model, "Logo image");
echo $form->fileField($model, 'image_logo2');
echo $form->error($model, 'image_logo2');
}
echo CHtml::button('Update', array('submit' => array("Template/update/?TemplateId=$TemplateId")));
// ...
$this->endWidget();
}
?>
POST 输出
Array
(
[image_logo1] =>
)