来自脚本文件夹外部的访问图像文件中的问题

Problem: Hard to synchronise image between 2 folder

My project has 2 image folder:

Home/user/public_html/image

Home/user/m.fruit.com/image

public_html serves the main page for desktop version

m.fruit.com serves the mobile page

Both version serves the same images.

When I have new fruit image, I need to put it into both folder. This becomes double work...

my way of accessing image for desktop version is: (index.php in public_html folder)

 <img id="image" src="image/<?php echo $image; ?>.jpg">

my way of accessing image for mobile version is: (index.php in m.fruit.com folder)

 <img id="image" src="image/<?php echo $image; ?>.jpg">

For mobile version, I did try to access image in public_html so that I don't need 2 image folder. But it seems like not possible:

   <img id="image" src="public_html/image/<?php echo $image; ?>.jpg">  

Is there any way to use only 1 image folder to serves these 2 version of website instead of 2 in this case?

Other better solution also welcomed.

First, you can set base URL: like

  define('BASE_URL', 'http://example.com/');

then set image src:

 <img src="<?php echo BASE_URL ?>image/imagename.jpg ">

or create a helper function: PHP CODE

function asset(path){
   return BASE_URL . path;
}

HTML CODE:

 <img src="<?php echo asset('image/imagename.jpg'); ?> ">

This will work on both sides.

Try to change the image url scheme with this

<?php echo "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; ?>

define base_url();

  define('base_url', 'http://Home/user');

and then call this base url where you want show image.

 <img id="image" src="image/<?php echo base_url('/public_html/').$image; ?>">