当前位置:网站首页>Laravel file stream download file

Laravel file stream download file

2022-06-26 04:22:00 Scholar and

	// Download file stream 
	//$file_name:test.docx( File name with suffix )
	//$file_dir:( Absolute path to file directory )
    public function downloadFile($file_name, $file_dir)
    {
    
        // Check if the file exists 
        if (! file_exists (  $file_dir . $file_name ))
        {
    
            header('HTTP/1.1 404 NOT FOUND');
        }
        else
        {
    
            // Open the file in read-only and binary mode 
            $file = fopen ( $file_dir . $file_name, "rb" );
            // Tell the browser that this is a file stream format 
            Header ( "Content-type: application/octet-stream" );
            // The unit of measure of the requested scope 
            Header ( "Accept-Ranges: bytes" );
            //Content-Length Is to specify the byte length of the data contained in the request or response 
            Header ( "Accept-Length: " . filesize ( $file_dir . $file_name) );
            // Used to tell the browser , Files can be downloaded as attachments , The name of the downloaded file is $file_name The value of the variable .
            Header ( "Content-Disposition: attachment; filename=" . $file_name );

            // Read the contents of the file and output it directly to the browser 
            echo fread ( $file, filesize ( $file_dir . $file_name ) );
            fclose ( $file );
            exit ();
        }
    }
原网站

版权声明
本文为[Scholar and]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202180533333730.html