gpt4 book ai didi

php - 无法使用 Yii 将文件上传到服务器

转载 作者:搜寻专家 更新时间:2023-10-30 23:10:22 26 4
gpt4 key购买 nike

我正在构建一个 YII 网络应用程序,我正在尝试添加一个用户可以将照片上传到网络应用程序的功能。我已经搜索了有关如何执行此操作的教程或文档,但没有成功。

有人告诉我,执行此类操作的最简单方法是将实际图像存储在服务器上的平面文件中,然后将图像的路径存储在数据库中。我创建了名为 Pictures 的模型具有以下属性:Title , url , & description .

这是我的 Controller 中创建操作的样子:

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/

最佳答案

试试这段代码。

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'];
$model->url = CUploadedFile::getInstance($model,'url');
if($model->save()) {
$fullImgSource = Yii::getPathOfAlias('webroot').'/granados/images/'.$model->url;
$model->url->saveAs($fullImgSource);
$this->redirect(array('view','id'=>$model->id));
}
}

$this->render('create',array(
'model'=>$model,
));
}

希望对你有所帮助。

谢谢

改进: block 在 if 语句之后丢失。

关于php - 无法使用 Yii 将文件上传到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21250637/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com