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);
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 want to save uploaded file to '/home/user/images' folder.
Is there any way to do this?
My 1st try:
controller
public function save(Request $request) {
$file = $request->file('image');
$file_name = $file->getClientOriginalName();
$file_path = '/home/user/images';
$file->move($file_path, $file_name);
}
/////////////////////
My 2nd try:
filesystems.php
'disks' => [
..
'custom_folder' => [
'driver' => 'local',
'root' => '/home/user/images',
],
...
controller
public function save(Request $request) {
$file = $request->file('image');,
$file_name = $file->getClientOriginalName();
Storage::disk('custom_folder')->put($file_name, $file);
}
I'm sorry if there is anything I did wrong. I just started learning php and Laravel.
For now, I save the files in the 'public / images' file path. I will use these files in multiple projects in the future. So I thought of such a method but could not reach the result.
if($request->hasFile('image')){
$image = $request->image;
$image_new_name = $image->getClientOriginalName();
$image->move('storage/custom_folder/', $image_new_name);
}
"storage" folder can be found inside "public" folder; public/storage/custom_folder.
If you want it outside your server dir then create a symlink inside your project folder.
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 am using below code to store the uploaded file
$file = $request->file($file_attachment);
$rules = [];
$rules[$file_attachment] = 'required|mimes:jpeg|max:500';
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
return redirect()->back()
->with('uploadErrors', $validator->errors());
}
$userid = session()->get('user')->id;
$destinationPath = config('app.filesDestinationPath') . '/' . $userid . '/';
$uploaded = Storage::put($destinationPath . $file_attachment . '.' . $file->getClientOriginalExtension(), file_get_contents($file->getRealPath()));
The uploaded files are stored in storage/app/2/filename.jpg
I want to show back the user the file he uploaded. How can i do that?
$storage = Storage::get('/2/filename.jpg');
I am getting unreadable texts. I can confirm that the file is read. But how to show it as an image to the user.
Hope i made my point clear.
Working Solution
display.blade.php
<img src="{{ URL::asset('storage/photo.jpg') }}" />
web.php
Route::group(['middleware' => ['web']], function () {
Route::get('storage/{filename}', function ($filename) {
$userid = session()->get('user')->id;
return Storage::get($userid . '/' . $filename);
});
});
Thanks to: #Boghani Chirag and #rkj
File not publicly accessible like you said then read file like this
$userid = session()->get('user')->id;
$contents = Storage::get($userid.'/file.jpg');
Assuming your file is at path storage/app/{$userid}/file.jpg
and default disk is local check config/filesystems.php
File publicly accessible
If you want to make your file publicly accessible then store file inside this storage/app/public folder. You can create subfolders inside it and upload there. Once you store file inside storage/app/public then you have to just create a symbolic link and laravel has artisan command for it.
php artisan storage:link
This create a symbolic link of storage/app/public to public/storage. Means now you can access your file like this
$contents = Storage::disk('public')->get('file.jpg');
here the file physical path is at storage/app/public/file.jpg and it access through symbolic link path public/storage/file.jpg
Suppose you have subfolder storage/app/public/uploads where you store your uploaded files then you can access it like this
$contents = Storage::disk('public')->get('uploads/file.jpg');
When you make your upload in public folder then you can access it in view
echo asset('storage/file.jpg'); //without subfolder uploads
echo asset('storage/uploads/file.jpg');
check for details https://laravel.com/docs/5.6/filesystem#configuration
Remember put your folder in storage/app/public/
Create the symbolic linksymbolic link to access this folder
php artisan storage:link
if you want to access profile images of 2 folder then do like this in your blade file
<img src="{{ asset('storage/2/images/'.$user->profile_image) }}" />
Laravel 8
Controller
class MediaController extends Controller
{
public function show(Request $request, $filename)
{
$folder_name = 'upload';
$filename = 'example_img.jpeg';
$path = $folder_name.'/'.$filename;
if(!Storage::exists($path)){
abort(404);
}
return Storage::response($path);
}
}
Route
Route::get('media/{filename}', [\App\Http\Controllers\MediaController::class, 'show']);
Can you please try this code
routes.php
Route::group(['middleware' => ['web']], function() {
Route::get('storage/storage_inner_folder_fullpath/{filename}', function ($filename) {
return Image::make(storage_path() . '/storage_inner_folder_fullpath/' . $filename)->response();
});
});
view file code
<img src="{{ URL::asset('storage/storage_inner_folder_fullpath/'.$filename) }}" />
Thanks
Try this:
Storage::disk('your_disk_name')->getDriver()->getAdapter()->applyPathPrefix('your_file_name');
Good Luck !
Uploaded like this
$uploadedFile = $request->file('photo');
$photo = "my-prefix" . "_" . time() . "." . $uploadedFile->getClientOriginalExtension();
$photoPath = \Illuminate\Support\Facades\Storage::disk('local')->putFileAs(
"public/avatar",
$uploadedFile,
$photo
);
and then access like this
<img src="{{ asset('storage/avatar/'.$filename) }}" />
Create Route:
Route::get('image/{filename}', 'HomeController#displayImage')->name('image.displayImage');
Create Controller Method:
public function displayImage($filename)
{
$path = storage_public('images/' . $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;
}
<img src="{{ route('image.displayImage',$article->image_name) }}" alt="" title="">
can You please try this
$storage = Storage::get(['type_column_name']);