A simple relative path issue that I can't resolve

Problems with the Windows version of XAMPP, questions, comments, and anything related.

A simple relative path issue that I can't resolve

Postby thedartgod » 26. February 2018 19:00

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?
thedartgod
 
Posts: 4
Joined: 26. February 2018 17:23
XAMPP version: win32-7.2.1-0
Operating System: Windows 10

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

Postby Nobbie » 27. February 2018 16:22

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.
Nobbie
 
Posts: 13170
Joined: 09. March 2008 13:04

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

Postby thedartgod » 27. February 2018 16:51

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.
thedartgod
 
Posts: 4
Joined: 26. February 2018 17:23
XAMPP version: win32-7.2.1-0
Operating System: Windows 10


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 78 guests