2015-07-10 124 views
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', 
     ); 
    } 
} 

回答

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, name varchar(255) NOT NULL, 电子邮件varchar(255) NOT NULL, 主题varchar(255) NOT NULL, text NOT NULL, 图像0​​created_on timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY ( ID ) )

+0

现在我收到此错误 致命错误:调用一个成员函数的saveAs()对空在d:\ PHP \ htdocs中\手动\保护\控制器\ SiteController .php 120行 – user1990386

+0

更改您的查看文件。因为我提供 –

+0

嘿感谢它的工作,现在你可以请让我知道我现在可以将它插入数据库。 – user1990386