我有上傳圖片的問題,它給我這個錯誤。警予2上傳文件未能打開流:沒有這樣的文件或目錄
move_uploaded_file(C:\AppServ\www\testyii\basic/uploadedfiles/1.jpg): failed to open stream: No such file or directory
根據這本書,我添加了這段代碼。在基本目錄
'aliases' =>
[
'@uploadedfilesdir' => '@app/uploadedfiles'
],
在基本/配置/ web.php $配置陣列
和創建的文件夾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]);
}
}
?>
並查看
<?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):無法打開流:沒有這樣的文件或目錄
請幫我解決這個問題。 非常感謝。
確保上傳目錄具有文件讀寫權限。 –