您好我正在尝试使用yii上传图片或文件,但它无法正常工作。我没有任何经验,请你帮助我。 这是我的代码提前致谢
这是控制器SiteController.php
public function actionCreate()
{
$model=new ContactForm;
if(isset($_POST['save']))
{
$model->attributes=$_POST['save'];
$model->image=CUploadedFile::getInstance($model,'image');
$path = Yii::app()->basePath . '\uploads';
//if($model->save())
// {
$model->image->saveAs($path);
// redirect to success page
// }
}
$this->render('index', array('model'=>$model));
}
这是我的视图文件
<?php
/* @var $this SiteController */
$this->pageTitle=Yii::app()->name;
?>
<h1>Welcome to <i><?php echo CHtml::encode(Yii::app()->name); ?></i></h1><div class="form">
<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'document-form',
'enableAjaxValidation' => false,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
'action' => array( 'site/create' )
));
?>
<?php
Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
$('.doc-form').toggle();
return false;
});
$('.doc-form form').submit(function(){
$.fn.yiiGridView.update('document-grid', {
data: $(this).serialize()
});
return false;
});
");
?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php //echo $form->errorSummary($model); ?>
<div class="row">
<?php //echo $form->labelEx($model, 'doc_name'); ?>
<?php //echo $form->textField($model, 'doc_name', array('size' => 50, 'maxlength' => 255)); ?>
<input type="text" name="doc_name">
<?php //echo $form->error($model, 'doc_name'); ?>
</div>
<div class="row">
<?php //echo $form->labelEx($model, 'doc_file'); ?>
<?php //echo $form->fileField($model, 'doc_file', array('size' => 36, 'maxlength' => 255)); ?>
<input type="file" name="doc_file//">
<?php //echo $form->error($model, 'doc_file'); ?>
</div>
<div class="row">
<?php //echo $form->labelEx($model, 'summary'); ?>
<?php //echo $form->textArea($model, 'summary', array('rows' => 6, 'cols' => 50)); ?>
<input type="text" name="summary">
<?php //echo $form->error($model, 'summary'); ?>
</div>
<div class="row buttons">
<?php //echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
<input type="submit" name="save">
</div>
</div>
<?php $this->endWidget(); ?>
这是我的模特
class ContactForm extends CFormModel
{
public $name;
public $email;
public $subject;
public $body;
public $verifyCode;
public $image;
/**
* Declares the validation rules.
*/
/*public function rules()
{
return array(
// name, email, subject and body are required
array('name, email, subject, body', 'required'),
// email has to be a valid email address
array('email', 'email'),
// verifyCode needs to be entered correctly
array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),
);
}
*/
// ... other attributes
public function rules()
{
return array(
array('image', 'file', 'types'=>'jpg, gif, png', 'safe' => false),
);
}
/**
* Declares customized attribute labels.
* If not declared here, an attribute would have a label that is
* the same as its name with the first letter in upper case.
*/
public function attributeLabels()
{
return array(
'verifyCode'=>'Verification Code',
);
}
}
答案 0 :(得分:0)
您的index.php视图文件如: -
<?php
$this->pageTitle=Yii::app()->name . ' - Contact Us';
$this->breadcrumbs=array(
'Contact',
);
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'contact-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
'htmlOptions' => array('enctype' => 'multipart/form-data'),
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'name'); ?>
<?php echo $form->textField($model,'name'); ?>
<?php echo $form->error($model,'name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'email'); ?>
<?php echo $form->textField($model,'email'); ?>
<?php echo $form->error($model,'email'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'subject'); ?>
<?php echo $form->textField($model,'subject',array('size'=>60,'maxlength'=>128)); ?>
<?php echo $form->error($model,'subject'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'body'); ?>
<?php echo $form->textArea($model,'body',array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($model,'body'); ?>
</div>
<div class="row">
<?php echo $form->fileField($model,'imagePath');?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Submit'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
您的ContactForm模型必须将CActiveRecord扩展为
<?php
class ContactForm extends CActiveRecord {
public $imagePath;
public function tableName() {
return 'contact_form';
}
public function rules() {
return array(
array('name, email, subject, body', 'required'),
array('imagePath', 'file', 'types'=>'jpg, gif, png', 'safe' => false),
array('id, name, email, subject, body, image, created_on', 'safe', 'on' => 'search'),
);
}
public function relations() {
return array(
);
}
public function attributeLabels() {
return array(
'id' => 'ID',
'name' => 'Name',
'email' => 'Email',
'subject' => 'Subject',
'body' => 'Body',
'image' => 'Image',
'created_on' => 'Created On',
);
}
public static function model($className = __CLASS__) {
return parent::model($className);
}
}
您的控制器操作必须
public function actionCreate() {
$model = new ContactForm;
if (isset($_POST['ContactForm'])) {
$model->attributes = $_POST['ContactForm'];
$model->imagePath = CUploadedFile::getInstance($model,'imagePath');
$model->image= Yii::app()->getBaseUrl(true).'/images/logoPhotos/'.$model->imagePath->name;
if($model->save()){
$path = Yii::app()->basePath . '/../images/logoPhotos/'.$model->imagePath->name;
$model->imagePath->saveAs($path);
$this->redirect($this->createUrl('login'));
}
}
$this->render('index', array('model' => $model));
}
您必须使用以下架构
在数据库中创建表 CREATE TABLE
CONTACT_FORM (
ID为int(11) NOT NULL AUTO_INCREMENT,
名称varchar(255) NOT NULL,
电子邮件varchar(255) NOT NULL,
受试者varchar(255) NOT NULL,
体text NOT NULL,
图像varchar(255) NOT NULL,
created_on timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (
ID为)
)