Avatar of Joseph Longo
Joseph Longo
Flag for United States of America asked on

Relative File Path Not Working - MacBook Pro

Hello Experts,
I am having an issue with my relative file paths. I am using a MacBook Pro, if that has any bearing, in case Mac's file paths are different than Windows.

Anyway, my root folder contains my index.php. I have no issues relating my index.php to any of the sub-folders/sub-files. However, when I want to relate a sub-file in one sub-folder to another sub-file in a different sub-folder, I seem to run into issues. I don't understand how to "back out" of one sub-folder and then access a different sub-folder.

See the image:

So, if I have a file in "PHP SUB-FOLDER 'B'" and I would like to link it to a file in the "FPDF FOLDER", would I do so like this:
../../fpdffolder/insert-the-file-name-and-extension-here

I have to back out two levels from "PHP SUB-FOLDER 'B'" in order to then access the fpdf folder, right?

What if I was in "PHP SUB-FOLDER 'B'" and wanted to access "PHP SUB-FOLDER 'A'". Would I do this?
../PHP SUB-FOLDER A/insert-the-file-name-and-extension-here
HTMLPHPMac OS X

Avatar of undefined
Last Comment
Joseph Longo

8/22/2022 - Mon
serialband

When you do .. to go to the parent directory, it only goes back up one level.  You need to add an extra ..
../../../fpdffolder/insert-the-file-name-and-extension-here

For your 2nd part: No.  You need to remove the subfolder name as you are already there.
../insert-the-file-name-and-extension-here
David Favor

Tip: There's a reason why CMS systems use absolute paths... always...

Here's how WordPress does this, looking at the bottom of a wp-config.php file off any WordPress site...

/** Absolute path to the WordPress directory. */
if ( !defined('ABSPATH') )
	define('ABSPATH', dirname(__FILE__) . '/');

/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php');

Open in new window


All code - WordPress core, theme, plugins all reference ABSPATH which ensures all path lookups are correct.

If you find yourself wrestling with relative path problems... loosing hours of time daily... switching to absolute paths will fix this.
Joseph Longo

ASKER
Okay, so say I am in PHP SUB-FOLDER B and I want to access a different folder under the PHP main folder.
So, for this example let's say PHP SUB-FOLDER A has Sub-Folders C, D, and E in it, too.

From PHP SUB-FOLDER B, how would I access subfolder E? I need to back out a few levels and then go down into the folder, right?

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
David Favor

You asked, "I don't understand how to "back out" of one sub-folder and then access a different sub-folder."

You provided no detail about what's different on the 2x system (Windows + OSX).

If you stick with 1x type of software, say Apache on both platforms, likely relative path problems will occur less.
serialband

Assuming your subfolders C,D, E are at the same level, to get to E under PHP SUB-FOLDER A from PHP SUB-FOLDER A, you need to go to:
cd ..\PHP SUB-FOLDER A\E\

If your subfolders are nested, then
cd PHP SUB-FOLDER B\PHP SUB-FOLDER C\PHP SUB-FOLDER D\PHP SUB-FOLDER E\

You might be getting confused about what the .. means.  It means parent directory, which is the directory above the current one.
Gerwin Jansen

Here's an example where I created your set of folders, the text in bold contains your first 2 links:

MBP:php user$ pwd
/Users/user/php
MBP:php user$ ls
css folder      fpdf folder      php folder
MBP:php user$ cd php\ folder/php\ sub-folder\ A/php\ sub-folder\ B/
MBP:php sub-folder B user$ ls ../../../fpdf\ folder/file1
../../../fpdf folder/file1
MBP:php sub-folder B user$ cd ~/php/php\ folder/php\ sub-folder\ A/php\ sub-folder\ B/
MBP:php sub-folder B user$ ls ../../php\ sub-folder\ A/file2
../../php sub-folder A/file2

Besides the / (slash) in unix/linux/macos and \ (backslash) in windows there is really no difference in walking a directory path.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Joseph Longo

ASKER
I should add that this is all for my website and I do not actually need to change directories on my Mac. I'm trying to link a few of my pages together but it's not working properly
Eoin OSullivan

If you're doing the links in PHP webpages then I think you're coming at the problem from the wrong direction.

You should list all paths relative to the root of the website .. not relative to each other.

For example ... if you have several PHP pages in nested folders as follows
/index.php
/PHP_FOLDER_A/index,php
/PHP_FOLDER_A/PHP_SUBFOLDER_B/index,php

And you want them all to load a logo located here
/MEDIA/IMAGES/photo.jpg

 .. they all use the same path relative to the ROOT
<img src="/MEDIA/IMAGES/photo.jpg">

You DO NOT try and map the relative path on the three different index.php files with different relative paths.  It makes no sense to have to code every single link to the same resource differently when you can do it relative to the ROOT of the website.
Joseph Longo

ASKER
I am not sure why this concept is so difficult for me to understand...but it is...

So, if I wanted to relate a file from PHP sub-folder "B" to a file in CSS sub-folder, I would do so from the root folder? That does not make sense to me. 
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
David Favor

1) Likely be helpful for you to provide a recursive directory listing for both your Mac + Windows system, starting at DocumentRoot.

find /docroot -type d | sort

Open in new window


This will make discussions much simpler.

2) Answering your specific question above, "if I wanted to relate a file from PHP sub-folder "B" to a file in CSS sub-folder, I would do so from the root folder?"

Seems simple.

/path-to-css-file

Open in new window


If you use absolute paths, then the same exact path works everywhere.

Makes no difference where you are in your DocumentRoot directory hierarchy.
ASKER CERTIFIED SOLUTION
Joseph Longo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.