I found the three-way to download files from storage such as
1.
Download the File From Storage Local Disk And Public Laravel
Laravel provides a convenient way to
download files from your server's local disk and public folder using the
Illuminate\Http\Request class. This method allows us to use the Request object
to retrieve a file from our application's storage directory (local disk) or the
public directory.
2. Download the File Using the Request Object
The request object contains a number of
useful methods that allow us to access information about the current HTTP
request. In this tutorial, we'll show how to use the get() method to retrieve a
file from the local disk or the public folder.
3. Download the File Using Request Class
In order to download a file from the
local drive or public folder, we need to create a new instance of the
Illuminate\Http\\Request class. Then, we can call the get() method on the
request object to retrieve a file.
Laravel allows you to store the file on a public disk and a Local Disk. This video is going to show you how to download files from storage public disk and a local
disk using Laravel, we will
1. check the condition if the file exists
2. define the path of the file to get the located path where the part store
5. use the function file_get_contents to get the content file
4. and download the file by using the response method.
AttachmentController.php for downloading files from local
The provided code is a PHP script that defines a class called "AttachmentController" within the "App\\Http\\Controllers" namespace. This class is responsible for retrieving files from storage and returning them as HTTP responses.
The class contains a single public method called "getFile". This method takes two parameters: `$path` and `$attachment_name`. The `$path` parameter represents the directory path where the file is stored, and the `$attachment_name` parameter represents the name of the file.
Inside the method, there is a check using the `exists` method of the `Storage` facade to determine if the file exists in the specified location. The `Storage::disk('local')` signifies that the local disk driver should be used for the file storage.
If the file exists, the method proceeds to retrieve the absolute path of the file using the `path` method of the `Storage` facade. It then reads the contents of the file using the `file_get_contents` function and assigns it to the `$content` variable.
The `ob_end_clean` function is called to clear the output buffer, ensuring that the response is not affected by any previous output.
Finally, the method returns a response containing the file's content using the `response` function. The `withHeaders` method is used to set the appropriate `Content-Type` header for the response, which is determined by the `mime_content_type` function based on the file's absolute path.
If the file does not exist in the specified location, the method redirects the user to the URL '/404', indicating that the file was not found.
In summary, the provided code demonstrates a simple implementation of a controller method that retrieves a file from storage and returns it as an HTTP response. It utilizes the Laravel `Storage` facade to interact with the file storage system and the `response` function to generate the appropriate response with the file's content and headers.
<?PHP
namespace App\Http\Controllers;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use League\CommonMark\Inline\Element\Strong;
class AttachmentController extends Controller
{
/**
* Retrieve file from storage
* @param string $attachment_name
* @return mixed
*/
public function getFile($path, $attachment_name) {
if(Storage::disk('local')->exists("attachments/$path/$attachment_name")) {
$absolutePath = Storage::disk('local')->path("attachments/$path/$attachment_name");
$content = file_get_contents($absolutePath);
ob_end_clean();
return response($content)->withHeaders(['Content-Type' => mime_content_type($absolutePath)]);
}
return redirect('/404');
}
}
AttachmentController.php for downloading files from public
The provided code is a PHP script that defines a class called "AttachmentController" within the "App\\Http\\Controllers" namespace. This class is responsible for handling file retrieval from storage.
The class imports several classes and namespaces using the `use` keyword. The `Exception` class is imported, which allows for catching and handling exceptions. The `Illuminate\Http\Request` class is imported, which represents an HTTP request. The `Illuminate\Support\Facades\Log` class is imported for logging purposes. The `Illuminate\Support\Facades\Storage` class is imported, which provides a simplified interface for interacting with the file storage system. Finally, the `League\CommonMark\Inline\Element\Strong` class is imported, but it does not appear to be used in the provided code.
The `AttachmentController` class extends the `Controller` class, indicating that it is a Laravel controller class.
The class contains a single public method called `getFile`. This method accepts two parameters: `$path` and `$attachment_name`. These parameters represent the directory path and the name of the file that needs to be retrieved.
Inside the `getFile` method, there is a check using the `exists` method of the `Storage` facade. This method checks if the file exists in the specified location. It uses the `local` disk driver to interact with the local file storage. If the file exists, the method proceeds with the retrieval process.
The code retrieves the absolute path of the file using the `path` method of the `Storage` facade. This method returns the full path to the file on the disk.
Next, the code uses the `file_get_contents` function to read the contents of the file. This function reads the entire content of a file and returns it as a string. The content of the file is stored in the `$content` variable.
The `ob_end_clean` function is called to clear the output buffer. This ensures that any previous output does not interfere with the response being sent.
Finally, the method returns a response containing the file's content using the `response` function. The `$content` variable is passed as the response body. The `withHeaders` method is used to set the appropriate `Content-Type` header for the response. It uses the `mime_content_type` function, which determines the MIME type of the file based on its absolute path.
If the file does not exist in the specified location, the method redirects the user to the `/404` URL, indicating that the file was not found.
In summary, the `AttachmentController` class provides a method called `getFile` that checks if a file exists in the specified location and retrieves its contents if it does. It then returns the file's content as an HTTP response with the proper headers. If the file does not exist, it redirects the user to a 404 page.
<?php
namespace App\Http\Controllers;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use League\CommonMark\Inline\Element\Strong;
class AttachmentController extends Controller
{
/**
* Retrieve file from storage
* @param string $attachment_name
* @return mixed
*/
public function getFile($path, $attachment_name) {
if(Storage::disk('local')->exists("attachments/$path/$attachment_name")) {
$absolutePath = Storage::disk('local')->path("attachments/$path/$attachment_name");
$content = file_get_contents($absolutePath);
ob_end_clean();
return response($content)->withHeaders(['Content-Type' => mime_content_type($absolutePath)]);
}
return redirect('/404');
}
}
No comments: