Jump to content

Trying to require a file in a laterally located directory using relative paths


DWilliams

Recommended Posts

I was under the impression this was a simple thing to do and I'm fairly certain I've done it before, but it isn't working for some reason.

 

Under the root directory of my project I have two subfolders called "inc" and "lib". lib contains third-party scripts for use in my project. The one in question is phpmailer (lib/class.phpmailer.php). Now, in inc I have a file that needs to include PHPMailer.

 

Inside my script in inc I have this:

require_once('../lib/class.phpmailer.php');

 

It errors out saying it can't find it. I thought that was supposed to go down one directory then back up into lib. The file does indeed exist.

 

What am I doing wrong? This seems like a stupidly simple thing...

Link to comment
Share on other sites

If you are including a file in the inc folder and that file then contains an include statement, the relative path used is that of the main file, because the first level included code just became part of the scope of the main file.

 

You are not including a file that includes a file. You are including code that becomes part of the main file and if that code contains an include/require statement that eventually gets executed it is relative to the main file.

Link to comment
Share on other sites

If you are including a file in the inc folder and that file then contains an include statement, the relative path used is that of the main file, because the first level included code just became part of the scope of the main file.

 

You are not including a file that includes a file. You are including code that becomes part of the main file and if that code contains an include/require statement that eventually gets executed it is relative to the main file.

 

Right but it's throwing the error for the class.phpmailer.php I try to include, not a file it subsequently tries to include.

 

Full error text:

Warning: require_once(../lib/class.phpmailer.php) [function.require-once]: failed to open stream: No such file or directory in /home/danny/workspace/smswebalerts/inc/send_email.php on line 3

Fatal error: require_once() [function.require]: Failed opening required '../lib/class.phpmailer.php' (include_path='.:/opt/lampp/lib/php') in /home/danny/workspace/smswebalerts/inc/send_email.php on line 3

Link to comment
Share on other sites

  • 4 weeks later...

Why not simply make the path to the library files non-relative?

 

With your library folder lib always under your root folder couldn't you just use:

 

require_once('/lib/class.phpmailer.php');

 

It would avoid any potential problems arising include files calling phpmailer where the include files are being used at different levels  e.g. /send_mail.php and /admin/send_mail.php

 

If you think the absolute path may be different across servers )e.g. your local test, and a production machine) you could set a variable

 

e.g.

define(‘ABSPATH’, dirname(__FILE__).’/');

require_once(ABSPATH.’wp-settings.php’);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.