cyril cyril
cyril cyril

Reputation: 1

Image directory

Hello I would like to do a function like "get_template_directory ()" from wordpress I did this :

define ('SITE_ROOT', dirname (__ DIR__, 1) .DIRECTORY_SEPARATOR. "public" .DIRECTORY_SEPARATOR);

  function getImageUri (){echo SITE_ROOT. '/ assets / img';}

Example for the logo "<img src ="<?php getImageUri ();?>/logo.png">"

the problem is that the returned url looks like this "file: /// C: /wamp64/www/mywebsite/public//assets/img/logo.png"

suddenly error (Not allowed to load local resource)

Any idea please?

Upvotes: 0

Views: 49

Answers (1)

&#193;lvaro Gonz&#225;lez
&#193;lvaro Gonz&#225;lez

Reputation: 146630

There're several types of paths:

  • A URL path is the convention you use to reach network resources using the HTTP protocol and typically start with http:// or https://, even if the browser decides to hide the prefix.

  • A file system path is the convention you use to reach files in your own computer's hard disk. On Windows they typically look like C:\Program Files\....

Just because you call them all "path" and they often look similar it doesn't mean they're the same thing or they're interchangeable.

The <img> tag is part of HTML, the core language of the web, so it doesn't use file system paths. You need to compose URL, not file system paths. However the __DIR__ magic constant retrieves a file system path, which is not what you want.

In your example, SITE_ROOT can be as simple as this:

define('SITE_ROOT', '/');
function getImageUri()
{
    return SITE_ROOT . 'assets/img';
}

... because you can normally omit the protocol, host and port fragments of the URL when already in your site's context.

Upvotes: 1

Related Questions