2014-01-07 14 views
5

私の目標は、最大のアップロードファイルの例外を管理し、クライアント側のメッセージを表示することですが、どこを制御するのが最適な場所であるのかわかりません。これは私のコントローラのメソッドです:ハンドルLaravel 4大きなファイルの例外をアップロード

public function upload_file() 
    { 
     if (!Input::hasFile('file')) 
      return; 

     $utils = App::make('utils'); 
     $file = Input::file('file'); 

     $name = Input::get('name'); 
     $size = $file->getSize(); 

     if ($size > FileModel::$max_file_size) 
      return json_encode(array('success'=>false, 'message'=>sprintf('The file size should be lower than %smb.',FileModel::$max_file_size/1000000))); 

     $original_file_name = $file->getClientOriginalName(); 

     $destination_directory = ""; 

     $final_file_name = $utils->copy_file_to_location($file); 

     return json_encode(array('success'=>true, 'file'=>$original_file_name)); 
    } 

そして、これはutilsのをcopy_file_to_location方法である:

public function copy_file_to_location($file, $destination_directory = "") 
    { 
     if (!isset($file)) 
      return; 
     $file_name = time()."_".$file->getClientOriginalName(); 

     $file->move(app_path().'/storage/files/'.$destination_directory, $file_name); 
     return $file_name; 
    } 

おろし金サイズよりを持つファイルをアップロードする場合に発生する例外を処理するために、どこがknwoませんサーバーの最大アップロードファイルサイズの変数。ユーザーフレンドリーなメッセージを表示するために、どこでどのようにこれを処理する必要があり、ユーザーインターフェイスをロックしないでください。ところで、私はExtJs 4をクライアント側で使用しています。ありがとう。



EDITは、私は多くのことを(それは同じ問題である)ことができます関連questionを見つけましたが、Laravelの内側に、私はこれをチェックすべきところ、私が知っている必要があります。

答えて

9

ファイルサイズがPHP変数upload_max_filesizeより大きく、2番目がpost_max_sizeより大きい場合は2つの場合があります。最初のものでは例外が発生しているので、それを捕まえるのは簡単な方法です。 2番目のケースでは例外はなく、私はそれを解決するためにthis質問を使用しました。

ここで、このコードをチェックしてください:Laravelコントローラのacitonメソッドで。コントローラのアクション内のコードは決して実行されなかったと思ったが、間違っていた。最終的にこれはこれを解決する方法です:

public function upload_file() 
    { 
     $file_max = ini_get('upload_max_filesize'); 
     $file_max_str_leng = strlen($file_max); 
     $file_max_meassure_unit = substr($file_max,$file_max_str_leng - 1,1); 
     $file_max_meassure_unit = $file_max_meassure_unit == 'K' ? 'kb' : ($file_max_meassure_unit == 'M' ? 'mb' : ($file_max_meassure_unit == 'G' ? 'gb' : 'unidades')); 
     $file_max = substr($file_max,0,$file_max_str_leng - 1); 
     $file_max = intval($file_max); 

     //handle second case 
     if((empty($_FILES) && empty($_POST) && isset($_SERVER['REQUEST_METHOD']) && strtolower($_SERVER['REQUEST_METHOD']) == 'post')) 
     { //catch file overload error... 
      //grab the size limits... 
      return json_encode(array('success'=>false, 'message'=>sprintf('The file size should be lower than %s%s.',$file_max,$file_max_meassure_unit))); 
     } 

     try{ 

      if (!Input::hasFile('file')) 
       return; 

      $utils = App::make('utils'); 
      $file = Input::file('file'); 

      $name = Input::get('name'); 
      $size = $file->getSize(); 

      if ($size > $file_max) 
       return json_encode(array('success'=>false, 'message'=>sprintf('El tamaño del archivo debe ser menor que %smb.',$file_max))); 

      $original_file_name = $file->getClientOriginalName(); 

      $destination_directory = ""; 

      $final_file_name = $utils->copy_file_to_location($file);  

      return json_encode(array('success'=>true, 'file'=>$original_file_name)); 
     } 
     catch (Exception $e) 
     { 
      //handle first case 
      return json_encode(array('success'=>false, 'message'=>sprintf('The file size should be lower than %s%s.',$file_max,$file_max_meassure_unit))); 
     } 
    } 
1

関連する質問に表示される設定 "upload_max_filesize"と "post_max_size"は、Laravelによって処理されていません。これは、PHPインストールにあるphp.ini設定ファイルの一部です。

1

古い投稿がまだ関連する問題です。 render()メソッドがHandler.php内部の私の方法だミドルウェアドキュメントhttps://laravel.com/docs/5.5/middleware#registering-middleware

チェック \を照らし\財団\のHttp \ミドルウェア\ VerifyPostSizeミドルウェア を使用してHandler.php内のエラーを処理:

public function render($request, \Exception $exception) 
     { 
     //thats the json your client will recieve as a response, can be anything you need 
      $response = [ 
       'code' => 500, 
       'status' => 'error', 
       'message' => 'Internal server error.', 
       'exception' => get_class($exception), 
       'exception_message' => $exception->getMessage(), 
       'url' => $request->decodedPath(), 
       'method' => $request->method(), 
      ]; 

      if (env('APP_ENV') === 'local') { 
       $response['trace'] = $exception->getTrace(); 
      } 

      if ($exception instanceof PostTooLargeException) { 
       $response['code'] = 413; //Payload too large 
       $response['message'] = $response['message'] .' The maximum request size is: ' .ini_get('post_max_size'); 
      } 

      return response()->json($response, $response['code']); 
     } 
関連する問題