2014-03-19 3 views
22

업로드 스크립트를 작성했습니다. Laravel 함수를 사용하여 Input :: file() 함수를 사용하고 있지만 여전히 null을 반환합니다. 홈 컨트롤러 코드를 게시 할 예정입니다. 내가 죽을 때Input :: file() null을 반환합니다. Laravel

public function handleUpload() 
    { 
     $user = Auth::user(); 
     $username = $user->username; 
     $userId = $user->id; 
     $path_to_users = "users"; 
     $user_profile_img = 'users/'.$username.$userId.'/'.$username.'image001.jpg'; 
     $user_path_profile = "users/$username$userId"; 
     $user_image_name = $username.'image001.jpg'; 
     if(file_exists($path_to_users)) 
     { 
      if(file_exists("$user_path_profile")){ 
       $file = Input::file('profile')->move("$user_path_profile/", $user_image_name); 
      } else { 
      mkdir("$user_path_profile"); 
      $file = Input::file('profile')->move("$user_path_profile/", $user_image_name); 
     } 

     } else { 
      mkdir("users"); 
      mkdir("$user_path_profile"); 
      $file = Input::file('profile')->move("$user_path_profile/", $user_image_name); 
     } 
    } 

은 (위해서 var_dump (입력 : 파일 ('프로필')))는 기본적으로 실제 파일로 내 파일에 복용되지 않음을 의미하는 null를 돌려줍니다. 내가 죽을 때 (var_dump (Input :: get ('profile'))) 이미지 업로드가 스크립트가 작동하지만 Laravel 백엔드가 실패했다는 것을 의미하는 이미지 이름을 반환합니다. 이게 사실인가요, 아니면 방금 뭔가 빠졌습니까? 당신이 무엇에 문제가 보인다, 그래서 그것은, 어쩌면보기를 작동합니다

@if($user->id == $userProf->id) 

          <div class="fileUpload button-success pure-button"> 
           <span>Upload</span> 
           <form action="{{action('[email protected]')}}" method="POST" enctype="multipart/form-data"> 
            <input type="file" name="profile" class="upload" /> 

           </form> 
          </div> 
          @endif 
+0

업로드 양식 HTML 코드를 여기에 게시 할 수 있습니까? –

+0

PUT 메서드를 사용하여 업로드 할 때 Laravel이 파일 요청을 처리 할 수 ​​없다고 생각합니다. 파일 업로드시 POST 메서드를 사용하십시오. –

답변

70

. 추가하는 것을 잊었습니다 :

enctype="multipart/form-data" 
3

코드를 보면 :

여기 내 HTML 양식 물건입니다. 난 그냥 여기를 테스트하기 위해 두 개의 라우터를 구축 :

Route::post('file', function() { 

    echo "<pre>"; 
    var_dump(Input::file('profile')); 
    echo "</pre>"; 

}); 

Route::get('upload', function() { 

    return Form::open(array('url' => '/file', 'files' => true)) . 
      Form::file('profile') . 
      Form::submit('upload'); 

}); 

당신은 단계별로 기준으로 사용할 당신이 너무 일을 할 수 있습니다.

26

나는 똑같은 문제가있어서 양식에 뭔가 빠져 있다는 것을 깨달았다. CRSF 보호 기능이 자동으로 추가되므로 내장 된 laravel 양식을 사용하십시오 (http://laravel.com/docs/html 참조). 어쨌든 - 폼의 상단에 'files' => true를 추가 할 필요가 -과 같이 블레이드 템플릿의 예를 들어 그것을 볼 수 있었다 : 나는 정상적인 형태로 같은 문제가 있었다

{{ Form::open(array('route' => 'admin.store', 'class'=>'form-horizontal', 'files' => true)) }} 

{{Form::file('file')}} 

{{ Form::submit('Submit', array('class' => 'btn btn-info')) }} 

{{ Form::close() }} 
+0

이렇게 쉽게 잊을 수 있습니다! 참조 용 문서 : http://laravel.com/docs/4.2/html#file-input – haakym

+0

저는 두 번이나이 작업을 수행했습니다.이 게시물은 두 번 나를 구해주었습니다. 감사합니다. @ Andrew – haakym

+0

@Andrew는 내가 어떻게 설명 할 수 있습니까? 정상적인 html 형식으로이 작업을 수행 할 수 있습니까? 나는 오래된 HTML을 재사용하고 그것을 laravel 프레임 워크와 섞어서 이미 너무 많은 코드를 바꿀 수는 없다. (jquery는 폼을 생성하기 위해 폼 id를 사용하고있다. laravel 생성 폼에 id를 추가 할 수 있는가? 또는 'files'=> true를 일반 html

태그에 추가 할 수 있습니까? –

관련 문제