Hi Dev I will show you how to fix image path in storage when hosting in server. I'm trying to access files that are stored in /storage/app/public/uploads/image/file.jpeg, but return is 404 which is not found. The problem is that the path in local machine is point different from path on hosting server.
I want to get display image via https://www.cambotutorial.com/uploads/test.jpeg. In hosting server point to uploads directory which is in root. But in local machine is acually point to /public/uploads. However, by default we upload files in Laravel are store in /storage/app/public/uploads/ which is different and can't link to display on webpage.
Finally, I have found a solution to solve this problem by using symlink and command artisan. I will guide you how to solve using symbolic link in a shared hosting and local machine.
If you store files in /storage/app/public/uploads and custome directory is uploads . The map link will point the files to public > uploads by following step below
Goto directory app->config->filesystems.php change path
'links' => [
public_path('storage') => storage_path('app/public'),
],
to
'links' => [
public_path('uploads') => storage_path('app/public'),
],
Then run command below
php artisan storage:link
After run artisan command will created new shortcut folder in public folder which is name uploads (/public/uploads), so we can access file within url http://localhost:8000/uploads/test.jpeg
In hosting server, in my case can not run command artisan, so below solution can handle above problem as well.
Create new symlink.php file in root directory (public_html) then run that file to link without run php artisan command.
symlink.php
<?php
$targetFolder = $_SERVER['DOCUMENT_ROOT'].'/storage/app/public';
$linkFolder = $_SERVER['DOCUMENT_ROOT'].'/uploads';
symlink($targetFolder,$linkFolder);
echo 'Symlink completed';
?>
Goto http://sample.com/symlink.php after run the file, it will create new shortcut folder in root directory name uploads.
We can access file within url http://sample.com/uploads/test.jpeg
You will see the link uploads directory are in differerent location. One is located in public folder and other one is located in root directory (public_html), but when you access the file path in hosting and local machine no need to changed which means you can access like http://sample.com/uploads/test.jpeg on both server and local machine.
Hosting Laravel PHP
As the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him