Requires file to destroy

I have a class and do this:

function __destruct() { $this->load_file('epicEndingfile.php'); } 

And I get the error message: Warning: require(...) [function.require]: failed to open stream: No such file or directory

And when I do the same with __construct , it works. Why is this?

EDIT: I really don't need the file, but I use a method for this.

+4
source share
3 answers

you need to use your CD (current directory), not the directory in which you put your file.

It may change in the context of your application (between construction and destruction). If you want to provide relative file paths based on your current file, use this:

 require dirname(__FILE__)."/epicEndingfile.php"; 

In PHP 5.2 and below and

 require __DIR__."/epicEndingfile.php"; 

In PHP 5.3+

+1
source

You need to calculate the location of the file. As you wrote it, the file must exist in the same directory as the file that calls the function. Add the full path and it should work.

 require '/full/path/to/file/epicEndingfile.php'; 
0
source

In the __destruct method, first do echo getcwd(); , and you will see the current working directory at this stage, I am sure that it has been changed at this point.

For example, if your class is defined in a file located in a different directory than your main script, the request will refer to a file that defines the class.

It is always a good idea to define a constant containing the base directory of your script. So, somewhere in the first lines of your main php file add.

 define('rootdir', dirname(__FILE__)); // you can replace dirname(__FILE__) with __DIR__ if it works in you PHP Version. 

Then each time you add or require a file that is relative to your main script file.

 include rootdir. '/requires/include.php'; 

eg.

0
source

All Articles