1
我正在構建YII Web應用程序,我試圖添加一個功能,用戶可以將照片上傳到Web應用程序。我已經搜索了關於如何做到這一點的教程或文檔,但沒有取得成功。無法使用Yii將文件上傳到服務器
我被告知最簡單的方法就是將實際圖像存儲在服務器上的平面文件中,然後將路徑存儲到數據庫中。我創建了模型,名爲Pictures
,屬性爲:Title
,url
,& description
。
下面是創建操作貌似在我的控制器:
public function actionCreate()
{
$model=new Pictures;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Pictures']))
{
$model->attributes=$_POST['Pictures'];
$uploadedFile=CUploadedFile::getInstance($model,'url');
if($model->save())
$uploadedFile->saveAs(Yii::app()->basePath.'/granados/images/testimage.jpg');
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
));
}
這裏是我的形式:
<?php
/* @var $this PicturesController */
/* @var $model Pictures */
/* @var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'pictures-form',
'htmlOptions' => array(
'enctype' => 'multipart/form-data',
),
// 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,
)); ?>
<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,'title'); ?>
<?php echo $form->textField($model,'title',array('size'=>60,'maxlength'=>128)); ?>
<?php echo $form->error($model,'title'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'url'); ?>
<?php echo CHtml::activeFileField($model, 'url'); ?>
<?php echo $form->error($model,'url'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'description'); ?>
<?php echo $form->textArea($model,'description',array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($model,'description'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
這裏是我的模型:
<?php
/**
* This is the model class for table "{{pictures}}".
*
* The followings are the available columns in table '{{pictures}}':
* @property integer $id
* @property string $title
* @property string $url
* @property string $description
*/
class Pictures extends CActiveRecord
{
/**
* @return string the associated database table name
*/
public function tableName()
{
return '{{pictures}}';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('title, url', 'required'),
array('description', 'length', 'max'=>500),
array('url', 'file','types'=>'jpg, gif, png'),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('id, title, url, description', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'title' => 'Title',
'url' => 'Url',
'description' => 'Description',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
*
* Typical usecase:
* - Initialize the model fields with values from filter form.
* - Execute this method to get CActiveDataProvider instance which will filter
* models according to data in model fields.
* - Pass data provider to CGridView, CListView or any similar widget.
*
* @return CActiveDataProvider the data provider that can return the models
* based on the search/filter conditions.
*/
public function search()
{
// @todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('title',$this->title,true);
$criteria->compare('url',$this->url,true);
$criteria->compare('description',$this->description,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
/**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* @param string $className active record class name.
* @return Pictures the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
如果任何人都可以指向正確的方向,那會很棒。此時,我無法上傳任何內容。當我提交新表單時,我收到了404錯誤。
我完全失去了這一點,不知道該怎麼做。以下是我迄今爲止試過的鏈接:
http://stackoverflow.com/questions/3607200/file-upload-with-yiis-activeform
http://stackoverflow.com/questions/10348887/show-uploaded-image-yii
http://www.yiiframework.com/wiki/2/how-to-upload-a-file-using-a-model/
工作完美!非常感謝您的幫助!!! – BlackHatSamurai