Page 1 of 1

A simple relative path issue that I can't resolve

PostPosted: 26. February 2018 19:00
by thedartgod
Using a local development environment with:
xampp-win32-7.2.1-0-VC15-installer
Windows NT SCDLSERVER 10.0 build 16299 (Windows 10) i586
Apache/2.4.29 (Win32) OpenSSL/1.1.0g PHP/7.2.1

My configuration has \\SCDLSERVER\xampp\htdocs\scdl as it's root. I have 3 files: header.html, index.html, and content\links.html

header.html is included (with php include) in index.html with include('header.html') and in content\links.html with include('../header.html').

header.html has an <IMG SRC="graphics/logo.jpg"...>.

When rendered from index.html it works. That is the image is looked for and FOUND in /scdl/graphics/logo.jpg as it should. When rendered from links.html it fails. That is the image is looked for and NOT FOUND in /scdl/content/graphics/logo.jpg.

Furthermore, links.html itself contains HREFs to other files. When rendered from index.html it works (looks for the files in /scdl/content); but does not work from links.html (looks for the files in /scdl/content/content).

PHP testing code indicates the full path is C:\xampp\htdocs\scdl in all cases.

I have googled this several times over the last 3-4 days and reviewed the postings here. All I found suggests it should be working as expected.

What am I missing?

Re: A simple relative path issue that I can't resolve

PostPosted: 27. February 2018 16:22
by Nobbie
thedartgod wrote:header.html is included (with php include) in index.html with include('header.html') and in content\links.html with include('../header.html').

header.html has an <IMG SRC="graphics/logo.jpg"...>.

When rendered from index.html it works. That is the image is looked for and FOUND in /scdl/graphics/logo.jpg as it should. When rendered from links.html it fails. That is the image is looked for and NOT FOUND in /scdl/content/graphics/logo.jpg.


Of course that does not work. It works exactly as designed. The IMG Tag is evaluated by the browser, whereas include() is evaluated and immediately executed by the server (PHP), which results in the same image TAG with SRC="graphics/logo.jpg" for both index.html and links.html. But index,html is stored in DocumentRoot(?) and links.html is stored (and delivered from) in subfolder "contents" (thats all what the browser knows, the browser does not know anything about PHP), therefore it is ok for index.html, but fails for links.html. You cannot do that in this way.

thedartgod wrote:Furthermore, links.html itself contains HREFs to other files. When rendered from index.html it works (looks for the files in /scdl/content); but does not work from links.html (looks for the files in /scdl/content/content).


For the same reason, href or src= is evaluated by the browser and the browser remembers the location of the referer (index.html or links.html are different referers and have different location). That leads to success for index.html, but not for links.html.

thedartgod wrote:PHP testing code indicates the full path is C:\xampp\htdocs\scdl in all cases.


?? You cannot test browser behaviour with PHP, its the wrong context?!

In your case you have to use absolute pathnames in src= and href= TAGs, if you need to refer to them from different folderlevels.

Re: A simple relative path issue that I can't resolve

PostPosted: 27. February 2018 16:51
by thedartgod
Thank-you. I'm just starting at this stuff and had expected a DUH! solution. In retrospect, it is; in fact, not a solution since it's working just the way it is suppose to. Now that I understand the context it should be easier [sic] going forward. Thanks, again.