Reputation: 31
first of all: Sorry for the title. If you can make something up to replace it, please do it since I just really didn't know how to name my problem in the first place. That said, there could very possibly be duplicates around, but I figured I need to get over with this thing and simply didn't know what to search for...
The base site wasn't made by me. It was written mostly in just HTML, but with inclusions done with PHP and I seriously don't like the way it was made. But this is getting irrelevant to the question I'm trying to ask here.
The files described here contain more stuff than what's written here, but the problem here is that
When requiring file A that requires file B that requires many files, file B can't find the files it's looking for.
So let's say I got a directory tree like:
| - Project
+--- lower.txt
| -- Stuff
| ---- Case
+------ index.php
+------ upper.php
| -- Gallery
+---- main.php
+---- config.php
+---- miscfunc.php
+---- css.php
Project\Stuff\Case\index.php:
<?php include("./upper.php");?> ...
<?php include("../../lower.txt");?>
Project\Stuff\Case\upper.php:
<?php require_once("../../Gallery/main.php"); ?>
Project\Gallery\main.php:
<?php namespace Gallery;
...
// I'm just trying to debug something here... :
// -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- //
$PathToScan = "./"; // Scan current directory, will point to /Project/Stuff/Case/
function ScanFolder($path) {
$Array = array();
if ($dir = @opendir($path)) {
while (($file = readdir($dir)) !== FALSE) {
$Array[] = $file;
}
printf("Current directory: %s<br/>\n", dirname(__FILE__));
printf("document_root: %s<br/>\n", $_SERVER["DOCUMENT_ROOT"]);
printf("Scan directory: %s<br/>\n<br/>\n", $path);
var_dump($Array);
}
}
ScanFolder($PathToScan);
// -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- //
require_once("./config.php"); // These files are also in Gallery namespace
require_once("./miscfunc.php");
require_once("./css.php"); ...
...
?>
So... when I execute Project\Stuff\Case\index.php I get:
Current directory: C:\xampp\htdocs\Project\Gallery
document_root: C:/xampp/htdocs
Scan directory: ./
array(4) { [0]=> string(1) "." [1]=> string(2) ".." [2]=> string(9) "index.php" [3]=> string(7) "upper.php" }
Warning: require_once(./config.php) [function.require-once]: failed to open stream: No such file or directory in C:\xampp\htdocs\Project\Gallery\main.php on line 44
Fatal error: require_once() [function.require]: Failed opening required './config.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\Project\Gallery\main.php on line 44
Upvotes: 1
Views: 371
Reputation: 31
I ended up changing
require_once("./filename.php");
rows to
require_once(__DIR__ . "/filename.php");
Still wondering if this could have been done otherwise...
Upvotes: 1