DWilliams Posted August 15, 2010 Share Posted August 15, 2010 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... Quote Link to comment https://forums.phpfreaks.com/topic/210795-trying-to-require-a-file-in-a-laterally-located-directory-using-relative-paths/ Share on other sites More sharing options...
PFMaBiSmAd Posted August 15, 2010 Share Posted August 15, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/210795-trying-to-require-a-file-in-a-laterally-located-directory-using-relative-paths/#findComment-1099588 Share on other sites More sharing options...
DWilliams Posted August 15, 2010 Author Share Posted August 15, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/210795-trying-to-require-a-file-in-a-laterally-located-directory-using-relative-paths/#findComment-1099641 Share on other sites More sharing options...
PFMaBiSmAd Posted August 15, 2010 Share Posted August 15, 2010 You are browsing to the inc/send_email.php file? Quote Link to comment https://forums.phpfreaks.com/topic/210795-trying-to-require-a-file-in-a-laterally-located-directory-using-relative-paths/#findComment-1099656 Share on other sites More sharing options...
DWilliams Posted September 9, 2010 Author Share Posted September 9, 2010 Alright so now I'm having the previously mentioned problem. It's throwing the error on a file being included by the file I initially include. What would be the proper way to handle this? Quote Link to comment https://forums.phpfreaks.com/topic/210795-trying-to-require-a-file-in-a-laterally-located-directory-using-relative-paths/#findComment-1109060 Share on other sites More sharing options...
DWilliams Posted September 10, 2010 Author Share Posted September 10, 2010 Bump Quote Link to comment https://forums.phpfreaks.com/topic/210795-trying-to-require-a-file-in-a-laterally-located-directory-using-relative-paths/#findComment-1109797 Share on other sites More sharing options...
stuartriches Posted September 11, 2010 Share Posted September 11, 2010 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’); Quote Link to comment https://forums.phpfreaks.com/topic/210795-trying-to-require-a-file-in-a-laterally-located-directory-using-relative-paths/#findComment-1109930 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.