0
votes

Tree:

--myproject
----mailer
-------class.phpmailer.php
----test
-------index.php
----site.php
----class.php
----db.php
----index.php

index.php: (both)

<?php 
require_once '../site.php';
?>

site.php:

<?php
require_once "class.php";
?>

class.php

<?php
require_once 'db.php';
require_once('./mailer/class.phpmailer.php');
?>

When I visit test it shows:

Warning: require_once(./mailer/class.phpmailer.php): failed to open stream: No such file or directory in C:\wamp\www\myproject\class.php on line 3


Fatal error: require_once(): Failed opening required './mailer/class.phpmailer.php' (include_path='.;C:\php\pear') in C:\wamp\www\myproject\class.php on line 3


I also tried include_once but same error!

3
First of all, why are you doing require_once '../site.php'; if both site.php and index.php are in the same directory?Rehmat

3 Answers

0
votes

You're calling the

require_once('./mailer/class.phpmailer.php');

From inside index.php, so the file is two levels above.

Change

require_once('./mailer/class.phpmailer.php');

to

require_once('../mailer/class.phpmailer.php');

UPDATE

Just realized you need both index.php files working. It's not the prettiest solution, but you can do it like this:

/test/index.php

<?php

$nested = true;
require_once '../site.php';

?>

/index.php

<?php 

$nested = false;
require_once 'site.php';

?>

This new "nested" variable will determine if the file is inside a directory or on the base.

Edit your class.php to look like this:

<?php

require_once 'db.php';

if ($nested == true)
require_once('../mailer/class.phpmailer.php');

elseif ($nested == false)
require_once('./mailer/class.phpmailer.php');

?>

This should fix the problem in both files.

0
votes

Let me try to answer this question.

On file index.php under folder test you can use code as follow:

<?php 
require_once '../site.php';
?>

On site.php you can use code as follow:

<?php
require_once "class.php";
?>

On class.php you can add code as follow:

<?php
require_once 'db.php';
require_once('mailer/class.phpmailer.php');
?>

On file index.php on root of folder myproject you can use code as follow:

<?php 
require_once 'site.php';
?>

I hope this trick can help up you.

0
votes

Use $_SERVER['DOCUMENT_ROOT'] and include the full path.

For example, on both index.php files you can do:

require_once($_SERVER['DOCUMENT_ROOT'].'/site.php');

And on site.php

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

And on class.php

require_once($_SERVER['DOCUMENT_ROOT'].'/db.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/mailer/class.phpmailer.php');

The reason why? Because $_SERVER['DOCUMENT_ROOT'] will dynamically return the full file path on the server to the document root. This is like saying (assuming your document root is /www/username/public_html):

require_once('/www/username/public_html/db.php');

Which will always be the same - until you change to a new directory location or server. When that happens, instead of re-writing every require_once(), because you have used $_SERVER['DOCUMENT_ROOT'], PHP will do the work for you.

Using a relative path can be confusing compared to a absolute path as it is relative to current location.