【问题标题】:Implement to File Storage Laravel实现文件存储 Laravel
【发布时间】:2017-08-26 10:59:30
【问题描述】:

我有一个具有 2 种方法(上传、单独)的类控制器。代码运行并没有任何问题。我的问题是如何在我的单独方法中实现 laravel 文件存储,因为在这种情况下,我使用 fopenfwrite 读写文件而不使用 Storage::GETStorage::PUT

    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, 'root@CLA-0 [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);

        }

}

【问题讨论】:

    标签: php laravel controller frameworks file-handling


    【解决方案1】:

    在控制器中创建一个公共静态函数并在其中添加代码,这样每次只需在控制器方法中调用静态函数即可。请注意,您也可以在方法中进行重构。

    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, 'root@CLA-0 [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);
    }
    

    【讨论】:

      猜你喜欢
      • 2019-11-11
      • 2018-12-12
      • 2020-05-09
      • 2016-11-27
      • 2018-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多