0

I want to define a constant for my images directory path by inserting the actual URL.

For example:

define("IMAGES_PATH", "http://example.com/images/");

Then I could link all my images dynamically from a database without having to know which subdirectory I am currently working in.

Then i will link to the image like this:

<img src="<?php echo IMAGES_PATH . $var; ?> alt="">

For the example i used $var = example.jpg as would be the result from the database.

So, Is this a legitimate way to do this?

M. Ullman
  • 41
  • 1
  • 8
  • I think the legitimate way to do this is to use some templating system like [twig](https://twig.symfony.com/) or [blade](https://laravel.com/docs/5.6/blade) – Karol Samborski Jul 10 '18 at 19:36
  • You can just do `src="/images/example.png"` – HTMHell Jul 10 '18 at 19:38
  • @HtmHell - That would only work if I am on the home page. But if I am on a different page for example: http://example.com/admin/products/add_new_product.php that wouldn't work. – M. Ullman Jul 10 '18 at 19:57
  • there is no reason NOT to do this it all depends on your project and situation. You could even put your define tag ( and others) in a config file and require it. – D. Simmons Jul 10 '18 at 20:23
  • D. Simmons - I have seen some posts about when linking internally that it is not good to insert the host name. Not sure why. (Maybe SEO?). Also, I don't understand why this wouldn't be an acceptable answer to this SO question (https://stackoverflow.com/questions/18220977/how-do-i-get-the-root-url-of-the-site/36103578#36103578?newreg=eb16f98e3a8f45039e1302c74396dbe9). – M. Ullman Jul 10 '18 at 20:55
  • @M.Ullman It would work, notice the [leading slash](https://webmasters.stackexchange.com/questions/56840/what-is-the-purpose-of-leading-slash-in-html-urls) – HTMHell Jul 10 '18 at 21:13
  • until he hits a sub domain. There is no seo reason you cant use the full url. It is perfectly fine to do what you have suggested. – ATechGuy Jul 10 '18 at 22:06
  • @HtmHell - Yeah, your right about the leading slash. (I don't know how I missed that!) Just I needed to make sure I started from the root dir. Though if I have a long path to my images I would want to define it. Maybe I will do this: `define("IMAGES_PATH", "/uploads/images/products/");` – M. Ullman Jul 11 '18 at 12:32
  • Sure, there is nothing wrong doing that, if you have a long path then it's even good. But you don't really need to enter a URL, you could just do, as in your last comment - a leading slash. (Also having a URL is not THAT bad, worst case you have to change it for development/production or if you change a domain. – HTMHell Jul 11 '18 at 12:34

0 Answers0