2016-08-14 5 views
0

양식이있는 이미지를 업로드하려고합니다. 총 5 개의 필드가 있으며 모든 필드를 필요로하지 않으려합니다. 그러나 필드 중 하나를 비워두면 예외가 있습니다. 하지만 5 개의 이미지를 모두 업로드하면 모든 것이 잘 작동합니다.Laravel 5 이미지 업로드

나는 $ rules 배열에 규칙이 없습니다. isValid()이 잘못되었습니다.
오류 :

FatalErrorException in ProfilesController.php line 191: 
Call to a member function getClientOriginalExtension() on a non-object. 

누군가가 올바른 방향으로 날 포인트 바랍니다.

내 컨트롤러 : 귀하의 코드가 비어 있거나 누락 된 파일에 대한 회계되지

public function update(ProfileRequest $request, $id) 
    { 
     $profile = Profile::findOrFail($id); 

     $request->merge([ 'wifi' => $request->has('wifi') ? true : false, 
          'takeaway'=> $request->has('takeaway') ? true : false, 
          'ec'=> $request->has('ec') ? true : false, 
          'creditcard'=> $request->has('creditcard') ? true : false, 
          'cash'=> $request->has('cash') ? true : false, 
          'wheelchair'=> $request->has('wheelchair') ? true : false, 
          'outdoor'=> $request->has('outdoor') ? true : false, 
          'tv'=> $request->has('tv') ? true : false, 
          'service'=> $request->has('service') ? true : false, 
          'smoking'=> $request->has('smoking') ? true : false, 
          'reservation'=> $request->has('reservation') ? true : false, 
          'brunch'=> $request->has('brunch') ? true : false, 
         ]); 





     // getting all of the post data 
     $file = array('image_profilehero' => Input::file('image_profilehero'), 
        'image_avatar' => Input::file('image_avatar'), 
        'pdf' => Input::file('pdf'), 
        'restaurantscene1' => Input::file('restaurantscene1'), 
        'restaurantscene2' => Input::file('restaurantscene2') 
      ); 

     // setting up rules 
     $rules = array(//'image_profilehero' => 'required', 
        //'image_avatar' => 'required' 

      ); //mimes:jpeg,bmp,png and for max size max:10000 

     // doing the validation, passing post data, rules and the messages 
     $validator = Validator::make($file, $rules); 
     if ($validator->fails()) { 
     // send back to the page with the input data and errors 
     return Redirect::to('backend/profile')->withInput()->withErrors($validator); 
     } 
     else { 
     // checking file is valid. 
     if (Input::file('image_profilehero') or Input::file('image_avatar')->isValid() or Input::file('pdf')->isValid() or Input::file('restaurantscene1')->isValid() or Input::file('restaurantscene2')->isValid()) { 
      $destinationPathAvatar = 'uploads/avatar'; // upload path Avatar 
      $destinationPathProfileHero = 'uploads/profilehero'; // upload path ProfileHero 
      $destinationPathPdf = 'uploads/speisekarten'; // upload path ProfileHero 

      //Restaurant Scene Bilder 
      $destinationPathRestaurantScene1 = 'uploads/restaurantscene'; // upload path RestaurantScene 
      $destinationPathRestaurantScene2 = 'uploads/restaurantscene'; // upload path RestaurantScene 

      $extensionAvatar = Input::file('image_avatar')->getClientOriginalExtension(); // getting image extension 
      $extensionProfileHero = Input::file('image_profilehero')->getClientOriginalExtension(); // getting image extension 
      $extensionPdf = Input::file('pdf')->getClientOriginalExtension(); // getting image extension 

      $extensionRestaurantScene1 = Input::file('restaurantscene1')->getClientOriginalExtension(); // getting image extension 
      $extensionRestaurantScene2 = Input::file('restaurantscene2')->getClientOriginalExtension(); // getting image extension 

      //$fileName = rand(11111,99999).'.'.$extension; // renameing image 

      $fileNameAvatar = '/avatar/avatar_'.Auth::user()->id.'.'.$extensionAvatar; 
      $fileNameProfieHero = '/profilehero/profilehero_'.Auth::user()->id.'.'.$extensionProfileHero; 
      $fileNamePdf = '/speisekarten/pdf_'.Auth::user()->id.'.'.$extensionPdf; 

      $fileNameRestaurantScene1 = '/restaurantscene/scene1_'.Auth::user()->id.'.'.$extensionRestaurantScene1; 
      $fileNameRestaurantScene2 = '/restaurantscene/scene2_'.Auth::user()->id.'.'.$extensionRestaurantScene2; 

      Input::file('image_profilehero')->move($destinationPathProfileHero, $fileNameProfieHero); // uploading file to given path 
      Input::file('image_avatar')->move($destinationPathAvatar, $fileNameAvatar); // uploading file to given path 
      Input::file('pdf')->move($destinationPathPdf, $fileNamePdf); // uploading file to given path 

      Input::file('restaurantscene1')->move($destinationPathRestaurantScene1, $fileNameRestaurantScene1); // uploading file to given path 
      Input::file('restaurantscene2')->move($destinationPathRestaurantScene2, $fileNameRestaurantScene2); // uploading file to given path 

      // sending back with message 
      return redirect('backend/profile')->with([ 
      'flash_message_important' => false, 
      'flash_message' => 'All done' 
      ]); 
     } 
     else { 
      // sending back with error message. 
      return redirect('backend/profile')->with([ 
      'flash_message_important' => false, 
      'flash_message' => 'Upps. Something's wrong.' 
      ]); 
     } 
     } 
     //return redirect('backend/profile'); 
     $profile->update($request->all()); 
    } 

답변

1

, 그냥 거기에 파일이있다 가정하고 move에 시도합니다. 따라서 객체가 아닌 객체를 호출하면 결국 null이됩니다. 어떤 PDF 파일의 경우 문이 거짓이 될 것이다을 존재하지 않는 경우는

$pdf = Input::file('pdf'); 
if ($pdf) { 
    $extension = $pdf->getClientOriginalExtension(); 
    $pdf->move($destinationPathPdf, $fileNamePdf); 
} 

이 방법 것입니다 : 당신은 당신이 이런 식으로, 그 메소드를 호출하기 전에 당신이 실제로 개체가 확인하기 위해 추가 작업을 조금 할 필요가 그걸 호출하는 메소드를 건너 뛰고, 그런 종류의 오류를 피하십시오. 이렇게하는 것이 일반적으로 좋은 습관입니다.