1

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:

Screenshot of the DOM in browser inspector Screenshot that proves that the image is correctly served

3
  • What is your file structure? Please add it to your question Commented Mar 28, 2017 at 9:18
  • 1
    try with http:// prefix in img src Commented Mar 28, 2017 at 9:20
  • simple image files Commented Mar 28, 2017 at 10:02

2 Answers 2

2

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

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

Sign up to request clarification or add additional context in comments.

1 Comment

Relative paths are easier to maintain.
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.