Jump to content

Path frustrations


DWilliams

Recommended Posts

It's the simple things that frustrate me sometimes.

 

My project has a one root folder. Under that are three separate folders (admin, img, and inc). "admin" contains admin pages, "img" contains images, and "inc" contains utility files to be included and used by other scripts.

 

I'll just take one specific problem as an example. In my inc folder is a file called "send_email.php". It needs to include another file called "class.phpmailer.php" in the same directory (inc). Now, since these files are never called directly but included by scripts in the rest of the project, I cannot simply do "require('class.phpmailer.php')".

 

I have a file in the base project directory called validation.php. It needs to include inc/send_email.php to use its functions to, obviously, send email. Now, that part is no problem. I just require('inc/send_email.php') since that is a valid path from validation.php. My problem comes when send_email.php tries  to include class.phpmailer.php. As I said before, I cannot just directly include it without any path since it's not being called directly. It "inherits" the path of the file that called it so just a direct require to it.

 

My "solution" was to add a config value called "path" to my config file in the root directory. It would ideally hold the path from the server root to my project folder. In my case I set it to "/danny/smswebalerts/" which is indeed the correct path from my web root. I use $config['path'] to link to my images and CSS files and it works correctly.

 

I thought it would solve my problem when I did the following in send_email.php:

 

require_once($config['path'] . 'inc/class.phpmailer.php');

 

That indeed correctly builds the full path to that file: /danny/smswebalerts/inc/class.phpmailer.php. When I open up a page that includes send_email, however, I get the following error:

 

Warning: require_once(/danny/smswebalerts/inc/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 20

 

I would appreciate if somebody could give me some nice solution before I just say "screw it" to organization and plop everything in the same directory. It's really frustrating that my project is being held up by some technicality like this.

Link to comment
Share on other sites

It is highly unlikely that 'danny' is within the OS's file system root which is where you are trying to include it from.

 

You will likely need to prepend the servers root path to this as well, using $_SERVER['DOCUMENT_ROOT'].

Link to comment
Share on other sites

It is highly unlikely that 'danny' is within the OS's file system root which is where you are trying to include it from.

 

You will likely need to prepend the servers root path to this as well, using $_SERVER['DOCUMENT_ROOT'].

 

Hmm I thought that require/include could use paths relative to the web server's root? I guess I should prepend it with DOCUMENT_ROOT then

 

My web server path setup is a bit misleading though, I don't know if that comes into play here. I have xampp installed. My files are really stored in /home/danny/workspace/smswebalerts but the web root is /opt/lampp/htdocs . /opt/lampp/htdocs/danny is a symlink to /home/danny/workspace . That shouldn't be causing any problems here though right?

Link to comment
Share on other sites

I had spent HOURS fighting with this kinda thing and when I discovered $_SERVER['DOCUMENT_ROOT'], life became sooooo much easier.

 

 

And keep in mind, that you can get out of site root with this too. I have a database class file that live outside of the web root for security... to grab it I use this.

 

include_once($_SERVER['DOCUMENT_ROOT'].'/../db.class.php');

 

This is essentially saying , start at SITE ROOT, go up 1 level and include the file. Very handy.

 

Nate

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.