1

I'm trying to include a PHP script which I'd like included in a folder 1 deep from the webroot. It is being triggered by a file 8 tiers deep into the site design. (*Adapting a Wordpress site with some server) . I'm using PHP 7.0.15

include('/Applications/MAMP/htdocs/thesite/GFScripts/'.$postid.'.php');  
//WORKS BUT IS A SLOPPY WAY OF CODING, CORRECT?

include(__DIR__ . '/../thesite/GFScripts/'.$postid.'.php');  
// Does not work

include(dirname(__FILE__) . '/../thesite/GFScripts/'.$postid.'.php');  
// Does not work

I've tried working with various stackoverflow comments but no luck so far. My main reference has been PHP include file in webroot from file outside webroot

Thanks for any help. I realise I could leave it as is, and change the directory once the site goes live. Though I'd like to write it in the best way and also understand why it is not currently working.

Jon Draper
  • 11
  • 3
  • You should start with a `var_dump(__DIR__);` to see the exact path of the current script. – jeroen Jun 26 '17 at 13:01
  • Thanks @jeroen can I ask why that will help?... That brings up: string(86) "/Applications/MAMP/htdocs/thesite/wp-content/themes/pro-child/framework/views/renew" / – Jon Draper Jun 26 '17 at 13:08
  • So you know how many directories you have to go up to get to `thesite/` directory? – jeroen Jun 26 '17 at 13:10
  • 1
    Ohh!... and doh!... and thankyou... I'd been thinking about it in a broken way. My brain has obviously failed me. I'd assumed that __DIR__ or dirname when accompaneid by the /../ went back to the webroot and so you could simply give the directory from there.... Thanks again: this works: **include(dirname(__FILE__) . '../../../../../../../GFScripts/'.$postid.'.php');** – Jon Draper Jun 26 '17 at 13:20
  • Aaaah, now I understand the confusion :-) – jeroen Jun 26 '17 at 13:22
  • By the way, there is a variable for the web-root: `$_SERVER['DOCUMENT_ROOT']`. But that only works if you actually use a web-server, so not from the command line. – jeroen Jun 26 '17 at 13:23

0 Answers0