Return images from storage folder - laravel

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

Related

Laravel storage get file causes problem in Google Chrome

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);

How to create a playlist of mp3 files from storage

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;
});

How to get image from resources in Laravel?

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.

How to get url from dropbox using flysystem?

Hello i has upload my file using laravel5, https://github.com/GrahamCampbell/Laravel-Dropbox integrate to dropbox and has succeed, and then i want to get the url for my imgsrc="" on the frontend, How i can get thats url?
dd(Flysystem::get('avatars/kenshin.jpg'));
Where is the url for imgsrc?
Assuming you already created a service provider for custom filesystem.
If you don't know how to do that the doc is Here
Route::get('/dropbox',function()
{
$filename = '/text1.txt';
$adapter = \Storage::disk('dropbox')->getAdapter();
$client = $adapter->getClient();
$link = $client->createTemporaryDirectLink($filename);
return <<<EOT
Link
EOT;
});
PLEASE NOTE THAT you have to prefix a "\" slash on the filename or else it will thrown an exception.
I save a shared link while saving a new resource. Like so
$path = Storage::disk('dropbox')->putFile('/images', storage_path('images/' . $imageName));
$adapter = \Storage::disk('dropbox')->getDriver()->getAdapter();
$client = $adapter->getClient();
$link = $client->createSharedLinkWithSettings($path);
$newsdigest = NewsDigest::create($request->all('title','type','source', 'article') + [
'reading_attachment' => $link['url']
]);
Create another route for a method in your controller and pass the file name that you want to access from dropbox with the route.
Use getFile() method in your controller and pass the filename to the variable.
public function getFile($file_name)
{
$client = new Client('dropbox.token','dropbox.appName');
$this->filesystem = new Filesystem(new Dropbox($client, '/path'));
try{
$file = $this->filesystem->read($file_name);
}catch (\Dropbox\Exception $e){
return Response::json("{'message' => 'File not found'}", 404);
}
$response = Response::make($file, 200);
return $response;
}
The best way I found to upload your file in Dropbox and even get share files link in your PHP or Laravel or any PHP framework is by using this package
composer require kunalvarma05/dropbox-php-sdk
This is how to use it, an example I made using Laravel:

Setting a config value dynamically in the controller in Codeigniter?

I'm trying to upload an image using Codegniter's Upload Library, the image should be uploaded to a directory which is created and returned by the model, do the upload directory changed every time, this is the code I'm supposed to use to change the config upload array:
if ($_FILES['image']['size']!=0) {
$path = $this->homeModel->createDirectories($id);
$this->config->load('upload');
$this->config->set_item('upload', array('upload_path' => $path));
// I've also used
// $this->config->set_item('upload_path', $path);
if (!$this->upload->do_upload('image'))
echo $this->upload->display_errors();
else {
$image = $this->upload->data();
$data['image'] = $image['file_name'];
}
}
but it doesn't seem to work. The upload_path doesn't change, the image is still uploaded in the path I've specified in config/upload.php file.
Try this:
if ($_FILES['image']['size']!=0) {
$path = $this->homeModel->createDirectories($id);
$this->config->load('upload', TRUE);
$config = $this->config->item('upload');
$config['upload_path'] = $path;
$this->upload->initialize($config);
//etc.
}

Resources