yii 2上传文件无法打开流:没有这样的文件或目录

时间:2017-04-07 02:21:06

标签: php yii

我上传图片时遇到问题,它会向我显示此错误。

  

move_uploaded_file(C:\的appserv \ WWW \ testyii \基本/ UPLOADEDFILES / 1.JPG):   无法打开流:没有这样的文件或目录

根据这本书,我添加了这段代码。

'aliases' =>
[
'@uploadedfilesdir' => '@app/uploadedfiles'
],

在basic / config / web.php的$ config数组中,在基本目录中创建文件夹uploadFiles

这是MODEL

<?php
namespace app\models;

use yii\base\Model;

class Room extends Model
{
    public $floor;
    public $room_number;
    public $has_condition;
    public $has_tv;
    public $has_phone;
    public $available_from;
    public $price_per_day;
    public $description;

    public $fileImage;

    public function rules()
    {
        return [
            ['floor','integer','min'=>0],
            ['room_number','integer','min'=>0],

       [['has_condition','has_tv','has_phone'],'integer','min'=>0,'max'=>1],
            ['available_from','date','format'=>'php:Y-m-d'],
            ['price_per_day','number','min'=>0],
            ['description','string','max'=>500],

            ['fileImage','file']
        ];
    }

    public function attributeLabels()
    {
            return [
            'floor' => 'Floor',
            'room_number' => 'Room number',
            'has_condition' => 'Condition available',
            'has_tv' => 'TV available',
            'has_phone' => 'Phone available',
            'available_from' => 'Available from',
            'price_per_day' => 'Price (EUR/day)',
            'description' => 'Description',
            'fileImage' =>'Image',
            ];
    }


}
?>

这是CONTROLLER

<?php


namespace app\controllers;

use Yii;
use yii\web\Controller;
use app\models\Room;
use yii\web\UploadedFile;

class RoomsController extends Controller
{
    public function actionCreate()
    {
        $model=new Room();
        $modelCanSave=false;

        if ($model->load(Yii::$app->request->post())&&$model->validate())
        {
            $model->fileImage = 
UploadedFile::getInstance($model,'fileImage');


            if($model->fileImage){

                $model->fileImage->saveAs(Yii::getAlias('@uploadedfilesdir/' 
.$model->fileImage->baseName. '.' . $model->fileImage->extension));

                //$model->fileImage-
>saveAs(Yii::getAlias('@uploadedfilesdir/' . $model->fileImage->baseName. 
'.' . 
 $model->fileImage->extension));
                //$model->fileImage->saveAs('@uploadFiles/' . $model-
>fileImage->baseName. '.' . $model->fileImage->extension);

                }
            $modelCanSave=true;
        }

        return $this->render('create',
['model'=>$model,'modelCanSave'=>$modelCanSave]);

    }
}
?>

和VIEW

<?php

use yii\helpers\Html;
use yii\helpers\Url;
use yii\helpers\ArrayHelper;
use yii\widgets\ActiveForm;
?>

<?php if($modelCanSave) { ?>
<div class="alert alert-success">
Model ready to be Saved!!
</div>
<?php } ?>

<?php $form=ActiveForm::begin();?>
<div class="row">
<div class="col-lg-12">
<h1>Room form</h1>
<?= $form->field($model,'floor')->textInput()?>
<?= $form->field($model,'room_number')->textInput()?>
<?= $form->field($model, 'has_condition')->checkbox() ?>
<?= $form->field($model, 'has_tv')->checkbox() ?>
<?= $form->field($model, 'has_phone')->checkbox() ?>
<?= $form->field($model, 'available_from')->textInput() ?>
<?= $form->field($model, 'price_per_day')->textInput() ?>
<?= $form->field($model, 'description')->textarea() ?>

<?= $form->field($model, 'fileImage')->fileInput()?>
</div>
</div>
<div class="form-group">
<?=Html::submitButton('Create',['class'=>'btn btn-success'])?>
</div>

<?php ActiveForm::end();?>

<?php if($modelCanSave) { ?>
<div class="alert alert-success">
Model ready to be saved!
<br /><br />
These are values: <br />
Floor: <?php echo $model->floor; ?> <br />
Room Number: <?php echo $model->room_number; ?> <br />
Has conditioner: <?php echo Yii::$app->formatter->asBoolean($model-
>has_condition); ?> <br />
Has TV: <?php echo Yii::$app->formatter->asBoolean($model->has_tv); ?> <br/>
Has phone: <?php echo Yii::$app->formatter->asBoolean($model->has_phone); ?> 
<br />
Available from (mm/dd/yyyy): <?php echo Yii::$app->formatter->asDate($model-
>available_from,'php:m/d/Y'); ?> <br />
Price per day: <?php echo Yii::$app->formatter->asCurrency($model-
>price_per_day,'EUR'); ?> <br />
Image:<?php if(isset($model->fileImage)) { ?> <img src="<?php echo 
Url::to('@uploadedfiledir/'.$model->fileImage->name)?>"/>
<?php } ?>
</div>
<?php } ?>

单击“创建”按钮时,它会显示给我。 move_uploaded_file(C:\ AppServ \ www \ testyii \ basic / uploadedfiles / 1.jpg):无法打开流:没有这样的文件或目录

请帮我解决这个问题。 非常感谢你。

1 个答案:

答案 0 :(得分:0)

您已设置别名文件夹名称不同。

'aliases' =>
[
'@uploadedfilesdir' => '@app/uploadedfiles'
],

在基本/ config / web.php的$ config数组中,在基本目录中创建文件夹 uploadFiles

所以你需要更新你的别名,如下所示。

'aliases' =>
[
'@uploadedfilesdir' => Yii::getAlias('@app').'/uploadFiles'
],