tags:

views:

74

answers:

5

Hi.

The problem is that I use the method file _ get _ contents("body.html") in a class that is in the same folder as body.html. The problem is that I get an error saying that the file could not be found. This is because I from another class require the file that uses the method file _ get _ contents("body.html"), and suddenly I have to use "../body/body.html" as filepath..!

Isn't that a bit strange? The class that calls the method file _ get _ contents("body.html") is in the same folder as body.html, but because the class is required from another class somewhere else, I need a new filepath?!

Here's a short list over directories and files:

lib/main/main.php
lib/body/body.php
lib/body/body.html

This is body.php:

class Body {

 public function getOutput(){
  return file_get_contents("body.html");
 }
}

This is main.php:

require '../body/body.php';

class Main {

 private $title;
 private $body;

 function __construct() {

  $this->body = new Body();
 }

 public function setTitle($title) {
  $this->title = $title;
 }

 public function getOutput(){
  //prints html with the body and other stuff.. 
 }
}

$class = new Main();
$class->setTitle("Tittel");
echo $class->getOutput();

The thing I ask for is a fix on the error that body.php is in the same folder as body.html but I have to change the path when a another class requires body.php from somewhere else in the method file _ get _ contents("body.html")

Thanks!

+1  A: 

No, this is not strange.

PHP is run with a working directory. This is commonly the directory the "entry" script lives in. This working directory does not change when you execute an included script.

If you want to read something in the directory of the currently executed file, try something like

$path = realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR."body.php";
gnud
+1  A: 

If you want to know what directory the current file is in, try dirname(__FILE__). You should be able to work from there.

Josh Bleecher Snyder
+1  A: 

You need to define a BASEPATH constant in the entry script... and then reference all files that are required by file_get_contents() relative to the BASEPATH

So it could be something like:

define("BASEPATH","/var/www/htdocs/");

//and then somewhere else
file_get_contents(BASEPATH."body/body.html");
pǝlɐɥʞ
+2  A: 

The scope of PHP's file-based functions always starts at the point of the first file in the execution stack.

If index.php is requested, and includes classes/Foo.php which in turn needs to include 'body/body.php', the file scope will be that of index.php.

Essentially, the current working directory.

You have some options, though. If you want to include/open a file in the same directory as the current file, you can do something like this

file_get_contents( dirname( __FILE__ ) . '/body.html' );

Or, you can define a base directory in a constant and use that for your inclusion

define( 'APP_ROOT', '/path/to/app/root/' );
file_get_contents( APP_ROOT . 'lib/body/body.html' );
Peter Bailey
+1  A: 

As an addition to the one who answered with dirname(FILE):

PHP 5.3 adds a DIR magic constant. So in PHP 5.3

file_get_contents(__DIR__."/body.html");

Should do the trick for you.

johannes