0
嗨,我想上传图像或文件使用yii,但它不工作。我没有任何yii.can的经验,请你帮助我。 这里是提前上传图像并保存在上传文件夹中
我的代码由于这是控制器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',
);
}
}
现在我收到此错误 致命错误:调用一个成员函数的saveAs()对空在d:\ PHP \ htdocs中\手动\保护\控制器\ SiteController .php 120行 – user1990386
更改您的查看文件。因为我提供 –
嘿感谢它的工作,现在你可以请让我知道我现在可以将它插入数据库。 – user1990386