laravel symlinks on a window machine - laravel

Using a windows server, I have a laravel installation.
I have a storage folder that I have created on 'root/storage'
When I save images from the website, the images are stored in this folder.
I use this to save the files:
Storage::disk('local')->put('course_badges/'.$pre_string . $request->file('badge')->getClientOriginalName(), File::get($request->file('badge')));
When I try to reference the file like so 'https://app.com/storage/file_name.jpg', laravel gives me a 'Sorry, the page you are looking for could not be found.' error.
However is I copy the files to '/public/storage/'
The image is then referenced correctly.
I have run the: php artisan symlink function.
I have tried to run ln -s ../storage/ command in powershell, but windows tells me that ln is not recognized.
What I am trying to achieve is to have my files saved in the root/storage folder (and sub folders)
and to reference them using https:app.nam/storage/file

I had simmilar issue
as u mentioned file is stored in root/storage means problem with symlink between root/storage to public/storage to fix this
remove storage folder from public dir
Note:- keep backup first
then run php artisan storage:link
it should work

Related

Storage symlink doesn't work with Laravel and Vue in Namecheap CPanel (LiteSpeed)

I am trying to access my image files stored inside storage/app/public/subfolder/ using symlink inside /public folder in my Laravel app. Everything works fine in my local setup and also in Jelastic Apache deployment, but it doesn't seem to work in CPanel with LiteSpeed. I am trying to access the files using the link /storage/subfolder/image.png but it is not accessible on CPanel deployment.
I tried creating and deleting storage symlink hundreds of times, but it didn't work. Below are few things I tried:
Deleting and creating symlink again and again using php artisan storage:link command.
Creating symlink using linux command ln -s ../storage/app/public storage inside public folder.
Running Artisan::call('storage:link') using a web route entry.
Redeploying and reconfiguring the app several times and creating storage symlink.
In all of these failed attempts I can verify that storage symlink is created every time inside public folder. I can navigate and view files from public/storage/ folder using CPanel terminal without any issues but can't access them in the deployed app.
I have exact same configuration in Jelastic Apache deployment and in my local deployment and there it is working without any issues.
UPDATE:
Additionally, I tried creating a storage directly manually inside public folder with the same structure and it worked. But symlink doesn't work.
UPDATE 2:
I found below error log while checking error logs from CPanel.
2022-11-06 05:07:07.342556 [ERROR] [1669834] [T0] [HTAccess] Failed to open [/home/user/my-project/public/storage/subfolder/.htaccess]: Permission denied
There is no .htaccess inside subfolder but it is there in my public folder.
What might be the issue?
After making linking you storage to your public path using storage:link you can access your folder :
'Storage_path('subfolder/image.png');'
After a long search, I finally found the cause.
Somehow the storage/app/public directory didn't have sufficient permissions. So, I gave permissions to that public folder recursively using chmod and everything works fine now.
The symlink was working fine but it was not working due to permission issues in the parent folder.
This is a bit weird as I have never manually altered permissions in any Laravel folders in any of my other deployments.

laravel storage link not working on another system

I have an existing laravel project that I wrote on a Windows PC, I tried to copy the project to a MacBook and it runs well, but the problem is the storage:link doesn't work, all the picture I save in the storage folder can't be accessed through the app, I tried to enter the link of the files manually on the browser and it's still doesn't work.
I also checked the folder and the files, they still exist.
I'm still new in using Mac, does anyone have a solution?
I also tried to upload my laravel project to a shared hosting without SSH access, how can I make a storage link?
Thanks in advance.
I am not sure, maybe this is for wrong file locations. well, if you can find the folders you want to link you can run a raw command anyway
ln -s /project_folder/storage/app/public /project_folder/public/storage
Once you move the project to another system or directory , you need to recreate the symlink. Re run the command
php artisan storage:link
I'm using a shared hosting service and move everything in laravel's public directory into hosting's public_html and move everything beside laravel's public directory into a new folder I generate in the same directory as the public_html directory such as below:
root / parent directory
public_html
laravel
Make a file in public folder named symlink.php which contains this code:
<?php
$targetFolder = '../laravel/storage/app/public';
$linkFolder = $_SERVER['DOCUMENT_ROOT'].'/storage';
if(symlink($targetFolder,$linkFolder)){
echo 'Symlink completed';
} else {
echo 'Symlink Failed';
}
?>
you can specify the $targetFolder variable to your storage/app/public path like above.
afterwards call the file through your laravel link.
eg:
http://mylaravel-example.com/symlink.php
nb : the file's name is not a fixity, you can specify it as you desired.

how do i solve the image upload issue of laravel while hosting it on shared hosting?

i am using hostinger to deploy my laravel project i uploaded all the files into the public_html folder and then did all the necessary changes needed for the application to work.The app seems to work pretty fine but when an user tries to upload a image the image gets stored in the storage/app/public/avatar folder and on public/storage/avatar folder as i have used symlink to link the two folders but yet the photos cannot be viewed by the users. this is how i recall my photo
div class="profile-avatar" style="width: 400px;height:500px; border-radius: 0%;background-image: url('#if(Auth::user()->avatar == "avatar.png") {{ url("/imgs/".Auth::user()->avatar) }} #else {{ url("/storage/avatar/".Auth::user()->avatar) }} #endif');"
please help me to solve this issue
As you have no ssh access to run some command in shell,
try this when uploading the image
1. Store image here: public_path() . 'img/filename.jpg'*
2. Save the 'img/filename.jpg' in database
3. Generate the image URL with url('img/filename.jpg')
// Output http://www.your-domain.com/img/filename.jpg
If you can manage shell access then try this:
Remove your current app/public/avatar directory, which is created inside of storage folder.
Also remove your storage folder inside public !
After that via ssh cli, cd to your laravel project folder and create a symbolic link via following command
ln -sr storage public/storage
Hopefully it will solve your issue. I had same issue and mine got fixed via second method ( I had ssh access to my server ).

Not Showing user avatar

HTML tag
<img src="storage/app/avatars/23_avatar1548246815.bmp" alt="">
File already exist on this path. But not showing.
And I used also this php artisan storage:link, but not working.
Error is Server Error 403 Forbidden.
According to laravel documentation.
The Public Disk
The public disk is intended for files that are going to be publicly accessible. By default, the public disk uses the local driver and stores these files in storage/app/public. To make them accessible from the web, you should create a symbolic link from public/storage to storage/app/public. This convention will keep your publicly accessible files in one directory that can be easily shared across deployments when using zero down-time deployment systems like Envoyer.
To create the symbolic link, you may use the storage:link Artisan command:
php artisan storage:link
Of course, once a file has been stored and the symbolic link has been created, you can create a URL to the files using the asset helper:
echo asset('storage/file.txt');
thing I would do is definitely use this method
<img src="{{asset('assets/images/image.png')}}">
to link to your asset files.
Shouldnt the storage/app/avatars be storage/app/pubic/avatars ?
If you say that the file exists on that path, it points that the problem is with file permissions. Change the /project/storage permissions to 777, delete the public/storage folder, and run php artisan storage:link, then try one more time. If no, try to give 777 permissions to public folder

Laravel - How to revert local storage symlink and refresh

When I save images to storage they show in the storage/app directory, but they don't show in public/storage. I also noticed that a storage/app/public directory seems to have been created which also contains everything within storage/app.
I can't work out how I managed to cause this mess, and I think it might make sense to revert these directories to how it should have been to begin with (with a new laravel project), remove any existing symlinks, and start again - does this sound like the best approach? And how would I go about doing this if I have set the symlink up incorrectly?
You can always delete the link:
cd public
rm storage
And create a new one without using the storage:link command if you need to link different locations:
\File::link(storage_path('dir1'), public_path('dir2'));
Or create a symlink manually:
ln -s /full/path/to/storage/dir1 /full/path/to/public/dir2
I had this problem and I fixed it by adding this to my deployment script:
cd public
rm storage
cd ..
php artisan storage:link
Before you do that, you can SSH into your deployment server and navigate to the /public folder. You might see a storage folder in there, and you should notice it's a symlink. You should leave it there and add my above commands to your deployment script.
If you remove storage link manually, you'll need to skip these on the first deployment:
cd public
rm storage
cd ..
Then every deployment after, you can do:
cd public
rm storage
cd ..
php artisan storage:link
If you don't do that, your deployment will fail probably because if rm storage throws an error, it will fail the build. Pay close attention to what I'm saying. You must be very specific when doing CLI commands, unless you can add some bash script that handles the 2 cases where ./public/storage exists (1) and when it doesn't (2).
You might gloss over my mention about deleting the storage symlink reference manually, but don't forget that if you deployment script does it and then fails for another reason, the symlink will be gone.
Someone should post an answer that uses a bash script to ensure both cases can be handled. I don't have the skill for that.
NOTE: php artisan storage:link only works while you are in the root directory of your project. That's why my above script starts with cd public and why it does cd ... If you aren't in the root at the time, the php artisan command will fail.
That is actually how it should be! Laravel has a command to symlink your public directory to the storage directory:
php artisan storage:link
The idea is to keep a persistent storage between deployments.
This convention will keep your publicly accessible files in one
directory that can be easily shared across deployments when using zero
down-time deployment systems like Envoyer.
https://laravel.com/docs/5.5/filesystem
Simple Way
Go to public folder Just delete/remove the storage link inside public/
Then Run the command
php artisan storage:link
the above command will link storage again
For windows users, I have tested this with Windows 10 and Laravel 7
Steps:
cd to your <project_root>/public directory and run rmdir storage - it will remove the link
cd back to project root directory and run php artisan storage:link to link again
The links should now be refreshed and viewable without issues.
php artisan storage:link --force

Resources