I'm using Dingo/API and this is a transformer that returns me data
return [
'id' => $site->id,
'path' => asset($site->path),
'siteLink' => $site->site_link,
'features' => $features,
];
Generated link looks good, however when I try to access it from my Angular app it's said that
Failed to load resource: the server responded with a status of 404 (Not Found)
http://api.example.com/public/thumbnails/ySOSYhaCCRcH3t9agsco3ToUwoHxMZJ3r1PhEHlM.jpeg
Are you sure that your image stored in public/public folder?
asset() helper generate a asset path from public folder. So, in your property $site->path you getting a path to your image like 'public/yourimage.jpeg'. Try to remove 'public' from your $site->path.
The solution.
In filesystems.php local disk was specified as a default disk. According to docs local disk is supposed to be invisible for public and stored inside storage/app. Thus I was trying to save my file using local disk and accessing it with path public/....
Option #1
Change in filesystems.php default disk to public and use Storage::url() to get url to image.
Option #2
Use $file->store('path', 'public') and Storage::disk('public')->url('path')
So that used disk is specified explicitly.
The given path will contain link close to http://example.com/storage/path.
Related
Within a Laravel project, I have an API that is powered by a flat JSON file. I'd like to use the JSON file where it currently resides, which is inside the /resources/modules/ folder in project. Problem is I'm having trouble targeting this location with the Storage:: method. Currently I have the file located in storage/app/public, which works fine with the code below, but ideally I'd reference the /resources/modules/ file.
$this->json_path = Storage::disk('public')->get('file.json');
$this->config_decoded = json_decode($this->json_path, true);
How do I do this using the Storage:: method?
Unless you've updated your public disk to point to the resources/modules directory, this is to be expected. By default, the public disk points to your storage/app/public directory.
You can either set up a new disk in your config/filesystems.php file by adding the something like the following to the disks array:
'modules' => [
'driver' => 'local',
'root' => resource_path('modules'),
'throw' => false,
],
Then your code would be:
$this->json_path = Storage::disk('modules')->get('file.json');
$this->config_decoded = json_decode($this->json_path, true);
Alternatively, you could use the File facade instead:
use Illuminate\Support\Facades\File;
$this->json_path = File::get(resource_path('modules/bob.json'))
I'm learning laravel but I have problem now with the image,
I want it to save it in storage/app/public/avatar so I can show it, but it keep saving it in storage/app/avatar
I write this in the .env
FILESYSTEM_DRIVER=public
And then I generate the storage link
php artisan storage:link
but still the same, every time I upload a photo it saving it in storage/app/avatar
Anyone can help me?
In filesystems.php
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
Here :
$attributes['avatar'] = request('avatar')->store('public/avatars');
In USer.php
public function getAvatarAttribute($value)
{
return asset($value);
}
this method was working correctly when i was using a random avatar link,
but now it show nothing, i check my database and the link is there and it's right
public function getAvatarAttribute($value)
{
asset('storage/avatars/'.$image->name);
}
$attributes['avatar'] = request('avatar')->store('public/avatars');
In the controller, update method:
$user->update($attributes);
$user->password = Hash::make($user['password']);
$user->save();
return redirect($user->path());
In the show.blade
<img
src= "{{ $user->avatar }}"
alt=""
class="rounded-full mr-4 absolute"
style="width: 150px; left: calc(50% - 75px); top: 300px"
>
but the src become "(unknown)"
This looks more like a configuration cache issue as your filesystem looks properly configured.
The storage/app folder is the path of laravel's default disk (the local disk). Since you had specified to use public as the default disk in .env but your application is still using the local disk path, it looks like a cache issue
Try using
php artisan config:clear
And then attempt
request('avatar')->store('avatars');
If it was indeed a cache issue, then the file should be stored in the storage/app/public folder.
To get the full path to the image, use
Storage::url('avatar/'.$user->avatar);
So in the accessor, use
public function getAvatarAttribute($value)
{
return Storage::url('avatar/'.$value);
}
Using asset() to get the path to the file is not recommended because if some day you wish to switch storage to the cloud (S3, or any other storage. basically, you want to not use the storage folder of your application), then the asset() will no longer give you the correct path and you'd need to change it everywhere. But when using the storage facade, all you need to do is change it in the filesystems.php
Also, just to test it yourself, you could also specify the disk when storing by specifying the disk as the second argument to the store method
request('avatar')->store('avatars', 'public');
Storage::disk('public')->url('avatar/'.$user->avatar);
Also, don't forget to delete public/storage folder
and then re-run
php artisan config:clear
This is important
php artisan storage:link
Change "FILESYSTEM_DRIVER=public" to
FILESYSTEM_DISK=public
Or remove that line completely and directly set public folder as default disk in filesystems.php as follows:
'default' => env('FILESYSTEM_DISK', 'public'),
The store method stores the files in storage/app directory. That's why your code is storing the image in storage/app/avatars
request('avatar')->store('avatars');
This will place it under storage/app/public/avatars
request('avatar')->store('public/avatars');
To show it
asset('storage/avatars/`.$image->name)
I am unable to delete the file (uploaded by the user) on calling the destroy method in the post controller.
I am able to upload the file to my drive that is
root/public/uploads/attachment
this is my filesystem
'uploads' => [
'driver' => 'local',
'root' => public_path('uploads'),
],
This is what i have tried in my controller
public function destroy(Post $post)
{
$post=Post::find($post->id);
Storage::disk('uploads')->delete($post->image);
$post->categories()->detach();
$post->tags()->detach();
$post->delete();
return redirect('admin/post')->with('message','Deleted Sucessfully');
}
I also have tried
unlink(public_path().'/uploads/'.$post->image);
But both actions gives the same results the post gets deleted but when i physically check the attachments folder the image is still present there
If you specify the Disk Then try it(Full Path of Image)..
Storage::disk('uploads')->delete(public_path().'/uploads/'.$post->image);
Or
Storage::delete($post->image);
And Must use
use Illuminate\Support\Facades\Storage;
You will need to use the full path to the file in order to delete it.
https://laravel.com/docs/5.8/filesystem#deleting-files
Try using the full path in Storage::disk('uploads')->delete($post->image);
I have created a designated location to store all the uploaded images in public dir like this:
and I have the default config/filesystem.php for public driver like this:
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
Within my repository, I am trying to save the uploaded image like this:
// ... snipped
if (array_key_exists('logo', $validated)) {
$logoPath = $validated['logo']->store(
'uploads/logos',
'public'
);
$company->logo = url($logoPath);
}
$company->save();
In the database, I can see the value of logo field for the company record like this: http://my-app.local/uploads/logos/3hlsAhnnIPjd4zhdpfhvKw4tqDkpcCz23NczwhVM.png
However, the public/uploads/logos dir is empty. Any idea what might be wrong here? Am I supposed to use the ->move() method on the UploadedFile instead of ->store()?
You can use an alternative method for uploading images in your public directory.
$request->validate([
'logo' => 'image|required',
]);
if($request->hasFile('logo')){
$company->logo = $request->image->move('uploads/logo', $request->logo->hashName());
}
$company->save()
Actually, yes, it was your mistake, but not the one you just found. By default laravel local storage points to storage/app/public directory, because laravel wants you to act professionally and store all uploaded files in storage, not in some kind of public directory. But then you would ask - how on earth user should access non-public storage directly? And there is a caught - php artisan storage:link command. It creates symbolic link from public directory to storage directory (storage/app/public -> public/storage). This is mostly because of deployments and version management.
Is it possible to write to a language file located at resources/lang/en/file.php from a controller? I moved my translations to database so user can edit them and now want to write the content from database to lang file each time content is changed.
You can write to anywhere in your filesystem provided you have the correct permissions. file_put_contents will write your contents to the desired path.
But you have the translation in a db, what's the use of writing to a file? You're just creating redundant data unnecessarily.
Edit:
You should define an entry in config/filesystems.php to use with the Storage facade.
'translations' => [
'driver' => 'local',
'root' => resource_path('translations'),
],
Found this function that helps me to write to resources folder:
app()['path.lang']