简体   繁体   中英

Issue in loading image file in PHP

I am facing some typical issue in loading image files in PHP. Image got successfully loaded but cannot get displayed properly.

I have declared a define value, then echoed it in an img tag src attribute :

define('image_site_url','localhost/projects/faantush/product_images/');
<img src="<?php echo image_site_url.$row['image1'] ?>" />

The image file is not found but it is there in product_images. Here are the pictures:

浏览器检查器中DOM的屏幕截图 屏幕截图证明了图像已正确投放

In the HTML code: You are missing the: http:// before localhost

<img src="http://localhost/ ...

If your pictures are hosted on the same server than your website, you don't really need to use an absolute path. You could use a relative path and remove the domain name ( localhost in this case):

<img src="/projects/path/to/your/picture.jpg" />

You must use absolute paths when linking to another website, but you can also use absolute paths within your own website. This practice is generally frowned upon, though. Relative links make it easy to do things like change your domain name without having to go through all your HTML pages, hunting down links and changing the names. As an added bonus, they force you to keep your site structure neat and organized, which is always a good idea.

An interesting use case of not using absolute paths is, if you want to set your website to SSL/TLS : you will have to change all the occurrences of http to https . Although it is not a big deal, this is generally not the kind of work you want to do.

See Relative path or url for html src and href attributes and the post from where this quote is taken .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM