Itsmoloco
Itsmoloco

Reputation: 31

../ on URL on php not working

this is kind of a silly question, but as I can't sort it out I thought it might be good to get some help. The point is that the ".. /" to go back directory is not working.

The file I'm executing is in a folder that's on the main route and I need to go back to the main route and then enter another folder to load this other PHP file but it's not working what could be causing this issue.

ERRORS:

Warning: require_once(../PHPMailer/PHPMailerAutoload.php): failed to open stream: No such file or directory in things/public_html/classes/Mail.php on line 3

Fatal error: require_once(): Failed opening required '../PHPMailer/PHPMailerAutoload.php' (include_path='.:/opt/alt/php71/usr/share/pear') in things/public_html/classes/Mail.php on line 3

DIRECTORY STRUCTURE:

File where the requiere once is:

/public_html/classes/filethatwantstoacces.php

File where it wants to get:

/public_html/PHPMailer/PHPMailerAutoload.php

require_once('../PHPMailer/PHPMailerAutoload.php');

Upvotes: 0

Views: 168

Answers (2)

FKEinternet
FKEinternet

Reputation: 1060

Given your location

/public_html/classes/filethatwantstoacces.php

doing ../ gives you

/public_html/classes

so ../PHPMailer/PHPMailerAutoload.php evaluates to

/public_html/classes/PHPMailer/PHPMailerAutoload.php

As @Martin has pointed out, using $_SERVER['DOCUMENT_ROOT'] to construct an absolute path to your file is the easiest way to avoid relative directory navigation errors such as this:

require_once $_SERVER['DOCUMENT_ROOT'].'/PHPMailer/PHPMailerAutoload.php';

Upvotes: 0

Martin
Martin

Reputation: 22760

What you should be using is the $_SERVER['DOCUMENT_ROOT'] variable. Please read this answer to another question for details.

If you are using PHP you should get into a habit of NOT using relative file paths at all but to use absolute paths, which will guarentee to succeed every time (As long as the target file exists and is reachable, etc.).

so; use $_SERVER['DOCUMENT_ROOT']

As a side note, you do not need to use brackets for your includes/requires, it's simply giving the server more work to do for no extra benefit.

The $_SERVER['DOCUMENT_ROOT'] is the base directory of your PHP/web application, typically the contents of the folder /public_html.

Using correct syntax and the above $_SERVER value (which will point to the /public_html folder you will have:

require_once $_SERVER['DOCUMENT_ROOT'].'/PHPMailer/PHPMailerAutoload.php';

This will work from any script within your directory structure, if the file (PHPMailerAutoload.php) exists and is reachable at that given location

Upvotes: 1

Related Questions