Linode Forum
Linode Community Forums
 FAQFAQ    SearchSearch    MembersMembers      Register Register 
 LoginLogin [ Anonymous ] 
Post new topic  Reply to topic
Author Message
 Post subject: PHP include_path issue
PostPosted: Tue Sep 13, 2011 3:17 am 
Offline
Senior Newbie

Joined: Sat Jul 11, 2009 10:38 am
Posts: 5
Hi, I have a problem with php include_path, all php includes are failing, something like:

include(‘Helper.php’);
include(‘Enchance.php’);
// Third party functions
include(‘misc.php’);

are all failing because php search in the wrong path and can’t find the files. Sure, adding absolute path works but I can’t do this on all files. Any suggestions ?
Just to be clear, it's not about my code, I've set up a web server on a linode and then installed wordpress, many plugins and themes give errors because of this. How to set include_path in php.ini (or maybe other settings) to not give errors when working with relative paths ?
Thank you.


Top
   
 Post subject:
PostPosted: Tue Sep 13, 2011 12:38 pm 
Offline
Senior Member
User avatar

Joined: Tue May 26, 2009 3:29 pm
Posts: 1691
Location: Montreal, QC
No PHP script should ever require include_path to be set in php.ini... If they do, it's bad code. You can use a relative path (like "mysite_includes/functions/foo.php") if needed.


Top
   
 Post subject:
PostPosted: Tue Sep 13, 2011 11:23 pm 
Offline
Senior Member
User avatar

Joined: Wed Apr 20, 2011 1:09 pm
Posts: 63
I would recommend using a constant with an absolute base path, like so:

(in your front-end files)
Code:
// index.php
define('ROOT_PATH', dirname(__FILE__));

// [...]

include ROOT_PATH . '/includes/mydirectory/myfile.php';


(deeper inside your application, front-end file in a subdirectory)
Code:
// subdirectory/index.php
define('ROOT_PATH', dirname(dirname(__FILE__)));

// [...]

include ROOT_PATH . '/includes/mydirectory/myfile.php';


You can also use that deeper in your code to maintain clear and understandable include paths, as well. Since all includes would be from the perspective of your forward-facing files, it'd mimic the behavior of using the include_path while still allowing yourself adaptability. To maintain your root path, just adjust your constant delcaration (and instead of using .. in the root path, use dirname to go up a directory in the string)

_________________
うるさいうるさいうるさい!


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
RSS

Powered by phpBB® Forum Software © phpBB Group