2017-08-26 191 views
0

我有一個類控制器2方法(上傳,單獨)。代碼運行並沒有任何問題。我的問題是如何在我的獨立方法中實現laravel文件存儲,因爲在這種情況下,我讀取和寫入文件與fopenfwrite不使用Storage::GETStorage::PUT執行文件存儲Laravel

class UploadController extends Controller 
{ 
    public function upload() 
    { 
     if(Input::hasFile('file')){ 
      $file = Input::file('file'); 
      $file->move('storage',$file->getClientOriginalName()); 
      $this->separate($file->getClientOriginalName()); 
      return view('layouts.pages.view_upload',['notice'=>$file->getClientOriginalName()." Uploaded"]);  
     } 
    } 

    public function separate($filename){ 

     $file_handle = fopen("../storage/app/public/WMLG2_2017_07_11.log", "r"); 

     //you 'll store your handles here 
     $targetHandles = []; 

     while (!feof($file_handle)) 
     { 
      $line = fgets($file_handle); 
      if (strpos($line, '[email protected] [WMLG2] >') !== false) 
      { 
       $namafileA = explode('> ', $line); 
       $namafile = str_replace(' ', '_', $namafileA[1]); 
       $filenameExtension = $namafile . ".txt"; 
       $file = preg_replace('/[^A-Za-z0-9\-_.]/', '', $filenameExtension); // hapus special character kecuali "." dan "_" 
      } 
      else 
      { 
       //no $file defined, most likely nothing to write yet 
       if (empty($file)) 
       { 
        continue; 
       } 

       //if its not open, we'll make them open 
       if (empty($targetHandles[$file])) 
       { 
       $targetHandles[$file] = fopen("../storage/app/public/show_command_file/$file", "a"); 
       } 
       //writing the line to target 
       fwrite($targetHandles[$file], $line); 
      } 
     } 

     //you should close your handles every time 
     foreach ($targetHandles as $handle) 
     { 
      fclose($handle); 
     } 

     fclose($file_handle); 

     } 

} 

回答

0

在控制器中創建一個公共靜態函數,並在其中添加代碼,因此每次只需在控制器方法中調用靜態函數。請注意,您也可以在方法中進行重構。

public function upload(){ 

    Static::uploadFile(); 

    return redirect(); //redirect somewhere 
} 

public function splitFile($filenane) 
{ 

$filename=request('file'); 

Static::separateFile($filename); 

return redirect(); //redirect somewhere 
} 




public static function uploadFile(){ 

if(Input::hasFile('file')){ 
      $file = Input::file('file'); 
      $file->move('storage',$file->getClientOriginalName()); 
      $this->separate($file->getClientOriginalName()); 
      return view('layouts.pages.view_upload',['notice'=>$file->getClientOriginalName()." Uploaded"]);  
     } 
} 

public static function separateFile($filename){ 

    $file_handle = fopen("../storage/app/public/WMLG2_2017_07_11.log", "r"); 

     //you 'll store your handles here 
     $targetHandles = []; 

     while (!feof($file_handle)) 
     { 
      $line = fgets($file_handle); 
      if (strpos($line, '[email protected] [WMLG2] >') !== false) 
      { 
       $namafileA = explode('> ', $line); 
       $namafile = str_replace(' ', '_', $namafileA[1]); 
       $filenameExtension = $namafile . ".txt"; 
       $file = preg_replace('/[^A-Za-z0-9\-_.]/', '', $filenameExtension); // hapus special character kecuali "." dan "_" 
      } 
      else 
      { 
       //no $file defined, most likely nothing to write yet 
       if (empty($file)) 
       { 
        continue; 
       } 

       //if its not open, we'll make them open 
       if (empty($targetHandles[$file])) 
       { 
       $targetHandles[$file] = fopen("../storage/app/public/show_command_file/$file", "a"); 
       } 
       //writing the line to target 
       fwrite($targetHandles[$file], $line); 
      } 
     } 

     //you should close your handles every time 
     foreach ($targetHandles as $handle) 
     { 
      fclose($handle); 
     } 

     fclose($file_handle); 
}