2017-04-07 39 views
0

我有上傳圖片的問題,它給我這個錯誤。警予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):無法打開流:沒有這樣的文件或目錄

請幫我解決這個問題。 非常感謝。

+0

確保上傳目錄具有文件讀寫權限。 –

回答

0

您已設置別名文件夾名稱是不同的。

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

基本/配置/ web.php配置陣列和創建的文件夾uploadFiles在基本目錄

所以,你需要更新你的別名,像下面。

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

我改變了以下建議,但它仍然無法正常工作,並顯示錯誤無效的路徑,如「無效路徑別名:@ uploadedfiledir/004.jpg」,但我感謝您的建議。 – Tman

+0

我修改了答案。請用新代碼更新您的代碼。 –

+0

在我更改以下更新後,出現如下錯誤。致命錯誤:未捕獲的異常 'YII \基\ InvalidParamException' 與消息 '無效路徑別名:@app':\的appserv \ WWW \ testyii \基本\供應商\ yiisoft \ yii2 \ BaseYii.php:152堆棧跟蹤:#0用C C:\的appserv \ WWW \ testyii \基本\ CONFIG \ web.php(58):警予\ BaseYii :: getAlias( '@應用')#1 C:\的appserv \ WWW \ testyii \基本\網絡\的index.php (10):要求( 'C:\\的appserv \\ WWW \\ ...')#2 {主}扔在C:\的appserv \ WWW \ testyii \基本\供應商\ yiisoft \ yii2 \ BaseYii.php上152行 – Tman

相關問題