1

I'm trying to write a PHP app and right now I'm having trouble with the include_once() method. I have a folder structure like this:

/home/header.php  
/home/index.php  
/home/admin/admin.php

I have no problem accessing header.php from index.php using include_once('/home/header.php'); it works perfectly, but if I try to call include_once() in admin.php with the same parameters, it crashes.

I want to use absolute paths just so I don't have to worry about where I call the function and all that. Am I doing something wrong with absolute paths? If so, what's the correct way to do it?

DidierFuentes
  • 87
  • 2
  • 17
  • 1
    Is the /home/admin folder readable for the user that runs the php script ? (Same question for the admin.php) – Armage Jun 09 '15 at 17:23
  • @Armage Not sure what you mean. I would assume they are accessible since I just created the folder like any other folder in eclipse for php. – DidierFuentes Jun 09 '15 at 17:30
  • just because you created it does not mean it's readable for the php script. You can use is_readable() to test that. – Armage Jun 09 '15 at 17:33
  • @Armage I did `var_dump(is_readable('/home/header.php');` in `/home/admin/admin.php` and it printed bool(false), so I'd guess I can't read it from there. – DidierFuentes Jun 09 '15 at 18:11
  • Re-reading your question (I've previously read you want to access admin.php from index.php...). But yes, your guess seems right :) – Armage Jun 09 '15 at 18:17
  • @Armage Do you know of any way to change this permission issue besides using `../` in my paths? – DidierFuentes Jun 09 '15 at 18:20

1 Answers1

0

Perhaps its advisable to use PHP magic function File which provides full path as a good practice for various reasons.

__File__

The full path and filename of the file with symlinks resolved. If used inside an include, the name of the included file is returned.

Ex:

require_once (dirname(__FILE__) . '/include.inc.php');

The following explains the differences for;

  • Include The include() statement includes and evaluates the specified file.

  • Include Once The include_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include() statement, with the only difference being that if the code from a file has already been included, it will not be included again. As the name suggests, it will be included just once.

  • Require require() and include() are identical in every way except how they handle failure. They both produce a Warning, but require() results in a Fatal Error. In other words, don’t hesitate to use require() if you want a missing file to halt processing of the page.

  • Require Once The require_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the require() statement, with the only difference being that if the code from a file has already been included, it will not be included again.

reference

Hope this helps.

daxeh
  • 1,083
  • 8
  • 12
  • also note `$_SERVER['DOCUMENT_ROOT'];` – SuperDJ Jun 09 '15 at 17:20
  • Ahh yes, thats another approach. Good point, I should get more info on the context and purpose. Cheers – daxeh Jun 09 '15 at 17:29
  • If I use `dirname(__FILE__)` in `/home/admin/admin.php` in the folder structure I have above I would get `/home/admin`.. What I really want to do is access something that is in `/home` from `/home/admin/admin.php` without using `../`. – DidierFuentes Jun 09 '15 at 18:17
  • How about defining base ie. root in an config file then include the config which then you have defined constants for root giving you the access to the paths you required. This means, multiple incudes. – daxeh Jun 09 '15 at 18:20
  • @AdrianTeh I tried that, but as mentioned in the comments to my question, I don't have permission to access an absolute path like that. – DidierFuentes Jun 09 '15 at 18:51