My all files path is /storage/app/public/applicants/pdf/[all-files-here]. So, how can I download it from the folder.
I have done this:
$file = Storage::disk('public')->get('/applicants/' . 'selection_test_result_1669368177.pdf');
return response()->download($file);
return Storage::download('file.pdf', $name, $headers);
Related
Looking at some samples how Laravel work with AWS S3 survice I see examples of code like :
$path = $request->file('image')->store('images', 's3');
But when I try to upload file from my local storage :
$adImageDestDirectory = '/public/' . ‘subdirectory_path’;
$path = Storage::get($adImageDestDirectory . $adImageItem->image)->store('images', 's3');
I got error:
Call to a member function store() on string
Which is valid way ?
Thanks!
If you need to upload files from local storage to aws S3 bucket then you need to copy files from local: use function file_get_contents()
Storage::disk('s3')->put('filename.jpg', file_get_contents($adImageDestDirectory );
You can do this to store the file to s3
$file = $request->file('attachment');
$fileName = time() . $file->getClientOriginalName();
$filePath = 'uploads/' . $fileName;
Storage::disk('s3')->put($filePath, file_get_contents($file), 'public');
$urlPath = Storage::disk('s3')->url($filePath);
I tried to get file from storage of my Laravel Application like following: (Notice: In Firefox Files in pdf / jpeg format are directly been showed. Other files like docx or something is causing download).
$contents = Storage::get("files/" . $file);
return $contents;
In Google Chrome file is not showing and just shows me source of file like this (Following is example if I have a jpeg image) My whole desktop is full of following signs:
ÿØÿà�JFIF������ÿÛ�„�
Do I have to set headers or something to show files in Google Chrome?
You can try instead :
$content = Storage::get("files/" . $file);
if (!File::exists($content)) {
abort(404);
}
$file = Storage::get($content);
$type = Storage::mimeType($content);
$response = Response::make($file, 200);
$response->header("Content-Type", $type);
return $response;
Try this.
$file = File::get("files/" . $file);
$type = File::mimeType("files/" . $file);
$response = Response::make($file, 200);
$response->header("Content-Type", $type);
I have some images in the storage folder. I want to get them in response and show to my users.
What I try:
I use the following code but it returns NULL:
// All images are in the storage/app/users/{id}/document folder
$path = storage_path( 'app/users/'.$id . '/document');
if (!File::exists($path)) {
abort(404);
}
$file = File::files($path);
$type = File::mimeType($path);
$response = Response::make($file, 200);
$response->header("Content-Type", $type);
return $response;
Output: Null
Where is my mistake?
Is there a better solution?
By the laravel documentation
you can resolve this.
return Storage::files('users/'.$id .'/document')
the default path of store file in laravel is storage/app/public and by default you should not set this path and your path is after this and laravel by default start from this path
and remember to add use Illuminate\Support\Facades\Storage;
in top of your class
I have uploaded my files with the storage and i have created records for each files informations in the database. Now I want to create a playlist and add a simple html audio player for each of them. how can I do this using the Storage filesystem? Is that possible at all or must I put all files to public?
something like this:
or this:
I'm not sure if I'm understanding your question correctly, but you can add a Route function to pull files out of the 'storage' folders with a simple Route entry:
// Used to retrieve the media files
Route::get('media/{filename}', function ($filename) {
$path = storage_path('app') . '/media/' . $filename;
if(!File::exists($path)) abort(404);
$file = File::get($path);
$type = File::mimeType($path);
$response = Response::make($file, 200);
$response->header("Content-Type", $type);
return $response;
});
I upload all user files to directory:
/resources/app/uploads/
I try to get image by full path:
http://localhost/resources/app/uploads/e00bdaa62492a320b78b203e2980169c.jpg
But I get error:
NotFoundHttpException in RouteCollection.php line 161:
How can I get image by this path?
Now I try to uplaod file in directory /public/uploads/ in the root:
$destinationPath = public_path(sprintf("\\uploads\\%s\\", str_random(8)));
$uploaded = Storage::put($destinationPath. $fileName, file_get_contents($file->getRealPath()));
It gives me error:
Impossible to create the root directory
You can make a route specifically for displaying images.
For example:
Route::get('/resources/app/uploads/{filename}', function($filename){
$path = resource_path() . '/app/uploads/' . $filename;
if(!File::exists($path)) {
return response()->json(['message' => 'Image not found.'], 404);
}
$file = File::get($path);
$type = File::mimeType($path);
$response = Response::make($file, 200);
$response->header("Content-Type", $type);
return $response;
});
So now you can go to localhost/resources/app/uploads/filename.png and it should display the image.
You may try this on your blade file. The images folder is located at the public folder
<img src="{{URL::asset('/images/image_name.png')}}" />
For later versions of Laravel (5.7 above):
<img src = "{{ asset('/images/image_name.png') }}" />
Try {{asset('path/to/your/image.jpg')}} if you want to call it from your blade
or
$url = asset('path/to/your/image.jpg'); if you want it in your controller.
Hope it helps =)
As #alfonz mentioned,
resource_path() is correct way to get the resource folder directory.
To get a specific file location, code will be like the following
$path = resource_path() . '/folder1/folder2/filename.extension';
First, change your config/filesystems.php
'links' => [
public_path('storage') => storage_path('app/public'),
public_path('resources') => resource_path('images'),
],
Then normally run
asset('resources/image.png')
you will get the file URL.