Daniel Capek
Daniel Capek

Reputation: 55

Php function file upload doesn´t work, how i can solve this?

I have a little problem with $_FILES prompt. This code i using in controller for upload images in user input ..

 public function actionAddequipment(){

  $model = new Equipment;
  $model -> equipment_title = $_POST['equipment_title'];
  $model -> equipment_sn = $_POST['equipment_sn'];
  $model -> equipment_type = $_POST['equipment_type'];
  $model -> user_id = $_POST['equipment_user_id'];
  $model -> equipment_utilities = $_POST['equipment_utilities'];
  $model -> equipment_date = date('Y-m-d H:i:s', Time());
  $model -> save();

   if (isset ($_POST['column'])){

     foreach($_POST['column'] as $key => $value){
        $model2 = new EquipmentColumnData;
     $model2 -> equipment_id = $model -> equipment_id;
     $model2 -> equipment_column_id = $key; 
     $model2 -> equipment_column_data_value = $value;
     $model2 -> save();

   }

  if(isset($_FILES)){

    if (!file_exists('../web/uploads/equipments/' . $model->equipment_id))
    mkdir('../web/uploads/equipments/' . $model->equipment_id, 0777,true);
    foreach($_FILES as $key => $value){
    $num=explode( '-', $key ); 
    if (!file_exists('../web/uploads/equipments/' . $model->equipment_id.'/'.$num[1]))
    mkdir('../web/uploads/equipments/' . $model->equipment_id.'/'.$num[1], 0777,true);
    move_uploaded_file($_FILES[$key]['tmp_name'], '../web/uploads/equipments/' . $model->equipment_id.'/'.$num[1]);


  }

  }



   }


}

}

But there is one problem. If i upload image, its all ok, command save, but image isnt in folder where i upload him. How i can solve this? Thanks for answers! :)

Upvotes: 1

Views: 47

Answers (1)

Amarnasan
Amarnasan

Reputation: 15549

Check the return value of move_uploaded_file, because I bet it is silently failing and returning a nasty false. Then check move-uploaded-file documentation and learn how to properly deal with file uploads.

Upvotes: 1

Related Questions