Laravel Storage Link Error
Common issue due to the way file systems are managed in shared hosting environments, where symbolic links might be restricted
Solutions and Workarounds
1. Check File Permissions
- Ensure that the
storage
andpublic/storage
directories have the correct permissions. - In cPanel or via SSH, set permissions for the
storage
directory:
chmod -R 775 storage
chmod -R 775 public/storage
This will give the web server write and read access to those directories.
2. Ensure Symbolic Link is Created
Run the following command (via SSH or the Terminal in cPanel):
php artisan storage:link
This command will create a symbolic link from public/storage
to storage/app/public
.
After running the command, check that the public/storage
directory exists and is properly linked to storage/app/public
.
3. Check .env File for APP_URL
For example, if your site is accessible at https://example.com
, ensure this is set:
APP_URL=https://example.com
If your APP_URL
is incorrect, Laravel may generate broken image URLs.
4. Use the Correct Path for Image URLs
When displaying images stored in the storage
folder, use the asset
or Storage::url
helper functions to generate the correct URLs.
Example using asset()
helper:
<img src="{{ asset('storage/images/your_image.jpg') }}" alt="Image">
Example using Storage::url()
:
<img src="{{ Storage::url('images/your_image.jpg') }}" alt="Image">
These will generate a URL pointing to the public/storage
directory where your files are stored.
5. Check .htaccess File for the Public Storage
If you’re using a .htaccess
file to manage URL rewriting, make sure there is no rule that could block access to the storage
folder in public
.
The following rule in your .htaccess
file (typically located in public_html
or the root) will ensure proper access:
# Ensure public access to storage folder
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^storage/(.*)$ /public/storage/$1 [L]
</IfModule>
6. Check the Image Paths in the Database (if applicable)
If you are storing image paths in a database, ensure that the paths stored are correct. You can test this by logging or dumping the path being used in your Blade templates:
{{ dd(asset('storage/images/your_image.jpg')) }}
This will help confirm if the correct path is being generated.
7. Clear Cache (Optional)
Sometimes, cached configurations or views may cause the images to not display. Clear the cache to see if that resolves the issue:
php artisan config:cache
php artisan view:clear
php artisan cache:clear
8. Debug Using the Browser’s Developer Tools
Use the developer tools in your browser (e.g., Chrome DevTools) to inspect the image paths being generated and to see if there are any errors in the console, such as 404 errors for missing files or permission-related issues.
Other Solutions and Workarounds
1. Manually Create the Symlink via SSH
- Access SSH via cPanel or a terminal.
- Navigate to the public directory where your Laravel project is stored: “cd public_html/public”
- Create a symbolic link manually using this command:
ln -s ../storage/app/public storage
This creates a symbolic link frompublic/storage
tostorage/app/public
. - Check if the symlink was created by navigating to
public_html/public/storage
. It should now point tostorage/app/public
. - Check Symbolic Link Existence using SSH Access
- Navigate to your
public
directory where the symbolic link should exist:
cd public_html/public - List the contents of the directory and check if the
storage
folder is a symbolic link:
ls -l - You should see an entry like this:
storage -> ../storage/app/public- The
storage
directory should point to../storage/app/public
. - If it doesn’t exist, you can try manually creating the symlink as mentioned earlier:
ln -s ../storage/app/public storage
- The
- Navigate to your
- Check Symbolic Link Existence using Via cPanel File Manager
If you don’t have SSH access, you can check using cPanel:- Log into cPanel, then open the File Manager.
- Navigate to the
public_html/mria.kloudbased.com/public
directory. - Check if the
storage
folder has a “link” icon next to it, indicating that it’s a symbolic link. - Verify the destination:
- Right-click on the
storage
folder in cPanel and select “Edit” or “View” to see where it points. It should point to../storage/app/public
.
- Right-click on the
- Delete the Existing (Broken) Symbolic Link
Before you can recreate the symbolic link, you need to remove the existing one that is broken or misconfigured.
Via SSH:- Navigate to the
public
folder of your Laravel application:
cd public_html/public - Remove the existing (broken) symbolic link:
rm -rf storage
This will remove the current storage folder, whether it’s a symbolic link or a regular folder.
- Navigate to the
- Via cPanel:
- Open cPanel, then go to the File Manager.
- Navigate to public_html/public.
- Locate the storage folder.
- Right-click on storage and select Delete.
- Make sure you delete only the symbolic link or broken folder and not any critical files.
- Recreate the Symbolic Link
Now, recreate the symbolic link.
Via SSH:
Create the symbolic link manually:
ln -s ../storage/app/public storage- This command creates a new symbolic link in the public folder that points from public/storage to storage/app/public.
- Via cPanel (No SSH Access):
If your hosting environment doesn’t allow you to use SSH to create symbolic links, and the php artisan storage:link command is not working, try the following alternatives:- Contact your hosting provider to ask them to create the symbolic link for you.
- Workaround: Instead of using a symbolic link, you can manually copy the contents from storage/app/public to public/storage as a temporary solution.
- Verify the Symlink
After creating the symlink, verify its existence.
Via SSH:
You can list the files in the public directory to confirm the link was created properly:- ls -l public_html/mria.kloudbased.com/public
- The output should show something like this:
storage -> ../storage/app/public
- This confirms that public/storage is now a symbolic link to storage/app/public.
Via cPanel:- Open File Manager in cPanel.
- Navigate to the public folder of your application.
- Check for the storage folder and see if it has a “link” icon next to it (this indicates it’s a symbolic link).
2. Workaround: Directly Serve Files from storage/app/public
- Copy the files from storage/app/public to public/storage manually:
This is a manual process but works as a workaround if symbolic links are not allowed.- Use the File Manager in cPanel or FTP to navigate to
storage/app/public
- Copy the contents of
storage/app/public
intopublic/storage
- Use the File Manager in cPanel or FTP to navigate to
- Modify File Upload Logic (optional): If you’re frequently uploading files, you can automate the copying of files from
storage/app/public
topublic/storage
using hooks in your Laravel code or by creating a scheduled task. - Update
.htaccess
for URL Rewriting
If you can’t create symbolic links, you can rewrite URLs so that files instorage/app/public
are served as if they were inpublic/storage
.- Edit the .htaccess file in your public_html/mria.kloudbased.com/public directory.
- Add the following rule to map requests for storage/* to the storage/app/public folder:
This.htaccess
rule will tell Apache to serve files from thestorage/app/public
folder when a request is made topublic/storage
.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^storage/(.*)$ ../storage/app/public/$1 [L]
</IfModule>
- Serve Files Using
Storage::url()
Ensure you are using the correct Laravel helper function to generate file URLs, especially if you’re working without a symbolic link. Instead of usingasset()
, useStorage::url()
to generate URLs for files stored in thestorage
directory:
This helper function automatically resolves the correct path based on your filesystem configuration.
<img src="{{ Storage::url('uploads/admin/AHJXTYMTQG/photo.png') }}" alt="Admin Photo">
- Check with Hosting Provider
Some shared hosting environments completely block the creation of symbolic links for security reasons. If none of the above steps work, contact your hosting provider to see if:- They can enable symbolic link creation for your account.
- They can provide an alternative or workaround.