1

I created Common Helper for Image Upload & Common Trait Function for get Images Full URL from Storage & I tested it in Laravel's default inbuilt server it's 1005 perfectly works.

but after Deployment I can't Display Images. it's show 404 error to me

I shared my Common function of Get Full URL path of Image from storage.

app/Traits>HasProfilePhoto.php file

public function getProfilePhotoUrlAttribute()
{
    return $this->profile_photo_path
                ? Storage::disk($this->profilePhotoDisk())->url($this->profile_photo_path)
                : \URL::to('/').'/adminLTE/dist/img/user-img.png';
}

above function will returns Image's Full URL Path Like."test.com/storage/users/user.png"

using Laravel's Default Inbuilt server it returns Full URL like this. http://localhost:8000/storage/users/user.png & using that path image display perfectly

but after the deployment in local Windows using XAMPP Server, it returns Full URL like this. http://localhost/ProjectFolderName/storage/users/user.png & it's show 404 Not Found

NOTE:- If I use FULL Url like this:- http://localhost/ProjectFolderName/public/storage/users/user.png then image displayed

Please Help me what's my mistake there?

* **How I deployed my Laravel Project see below:- **

*

root/server.php file renamed with index.php

& root/public/.htaccess file copied at root directory

Harsh Patel
  • 1,032
  • 6
  • 21
  • 1
    Your laravel public dir should be the domains root dir. Side note: XAMPP might not be the best option for a Laravel dev env (it's better to use `php artisan serve` for example ) – Gert B. Sep 07 '21 at 10:12
  • @GertB. please can you share full detail example about deployment? – Harsh Patel Sep 07 '21 at 10:34

2 Answers2

1

Rename the server.php file to index.php is not good practice for security reasons as well. So if you need to hide the public path from the URL then create a .htaccess at root level:

<ifmodule mod_rewrite.c>

    <ifmodule mod_negotiation.c>
        Options -MultiViews
    </ifmodule>
 
    RewriteEngine On
 
    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ ^$1 [N]

    RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
    RewriteRule ^(.*)$ public/$1 

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ server.php

</ifmodule>

Updated by Harsh Patel:- 8th September 2021

Actually Problem is about defining Virtual Host in Server... I was defined DocumentRoot path & Directory path as like "C:/xampp/htdocs/projectFolderName/ this.

I was identified my mistake in deployment from here medium's Blog

but as per Laravel Documentation , we need to direct all requests to public/index.php in other words Laravel's Public Folder is an Our Server's Root (public_html) folder that's why I need to define DocumentRoot & Directory up to public folder like this C:/xampp/htdocs/projectFolderName/public

  • We need to define our Laravel Web in Apache Server's Virtual Host Config.

for XAMPP Server

step 1: open C:\xampp\apache\conf\extra path location in XAMPP Server

step 2: Open httpd-vhosts.conf File

step 3: define virtual host rule as like below

<VirtualHost *:80>
  ServerName example.com
  ServerAlias www.example.com
  DocumentRoot "C:/xampp/htdocs/projectFolderName/public"
  <Directory "C:/xampp/htdocs/projectFolderName/public">
    Options +Indexes +Includes +FollowSymLinks +MultiViews
    AllowOverride All
    Require all granted
  </Directory>
</VirtualHost>

IF you faced any problem to define Virtual Host in XAMPP then you can see Full detailed answer about How to define Virtual Host in XAMPP

& If you Use WAMPP Server then you can see Full detailed answers about How to define Virtual Host in WAMPP

Amit Senjaliya
  • 2,867
  • 1
  • 10
  • 24
  • I already created symbolic of storage path at public path using command. & Storage::disk('public)->get('file.png') is get file or Image from blade template Or respond it in API but I want to pass Full Image Path which is located at storage. then using that path browser will get image automatically – Harsh Patel Sep 07 '21 at 10:31
  • no I can't access using Image using Manually in Browser. it's display 404 not found. & brother I deploy Laravel web in Our Server PC (OS:- Windows) locally. If I use laravel's default server then I can access manually also – Harsh Patel Sep 07 '21 at 10:43
  • function returns that path http://localhost/microtechcoupon/storage/users/user16148609746040d2ae9dd81.png but its show 404 error even I can't access manually in browser using this link.... But But When I try to http://localhost/microtechcoupon/public/storage/users/user16148609746040d2ae9dd81.png this link manually in browser then its works... So I think there was I made any mistake in deployment... otherwise function works properly – Harsh Patel Sep 07 '21 at 10:50
  • yes correct. but as I know we can access storage file directly without mention public before the storage URL & it works in default server, not works in deployment – Harsh Patel Sep 07 '21 at 10:56
  • can you tell me full details about how to deploy in Linux personal host, Linux Shared host & as well as how to deploy in local using xampp server??? – Harsh Patel Sep 07 '21 at 10:57
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/236843/discussion-between-harsh-patel-and-amit-senjaliya). – Harsh Patel Sep 07 '21 at 11:00
  • yes I know, we can't interact direct without symbolic. that I already done. my meaning was without mention public in url – Harsh Patel Sep 07 '21 at 11:02
  • & asset() helper returns path upto public folder. & my function also returns full path. but its not work – Harsh Patel Sep 07 '21 at 11:05
  • Brother I have edited your answer. You can see exact solution of my question & I also accept your answer ... Thank you for your valuable time – Harsh Patel Sep 08 '21 at 05:02
  • @HarshPatel I have seen the edited answers. But when u set public folder root then no need to rename server.php to index – Amit Senjaliya Sep 08 '21 at 05:04
  • 1
    yes Exactly if we define public folder as a root directory then we did't need to rename server.php with index.php & as well as we didn't need to copy public/.htaccess file at root directory – Harsh Patel Sep 08 '21 at 05:07
1

Maybe you just need to set your projects URL inside .env to start with http://

APP_URL=http://your-project-url

Without this, images will not have proper URL but when you copy image path and open it inside new tab, image will be available anyway.

  • No, your opinion is correct but I was set APP_URL properly in .env configuration file. there is no issue in my case. I already edited answer of my problem in Amit Senjaliya's answer,. I have already solved my problem. Thanks – Harsh Patel Feb 21 '22 at 04:03
  • It's nice to hear that, in case someone else has a similar problem, it may help, since I've been looking for a solution for a while. You are welcome – Mirko Dzudzar Feb 22 '22 at 10:52