Link to home
Start Free TrialLog in
Avatar of Joseph Longo
Joseph LongoFlag 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:
User generated 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:

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
Avatar of serialband
Flag of Ukraine image

When you do .. to go to the parent directory, it only goes back up one level.  You need to add an extra ..

For your 2nd part: No.  You need to remove the subfolder name as you are already there.
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.
Avatar of Joseph Longo


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?

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.
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:

If your subfolders are nested, then

You might be getting confused about what the .. means.  It means parent directory, which is the directory above the current one.
Here's an example where I created your set of folders, the text in bold contains your first 2 links:

MBP:php user$ pwd
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.
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
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

And you want them all to load a logo located here

 .. 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.
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. 
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.


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.
Avatar of Joseph Longo
Joseph Longo
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial