Skip to main content
gamedev.net
🔒 Locked

[web] PHP include / file path problems

Started by leiavoia Aug 31, 2006 at 10:48 AM 4 replies 11.9k views
Original Post
leiavoia
leiavoia
I have run into a relative path issue in PHP i was hoping you may have dealt with yourself before. Here is the problem: File A contains utility function that read/write files. FILE A: ------------------------- function Foo() { fwrite( "./somefile.txt", "bla" ); } File B includes file A for it's functions FILE B: ------------------------- require_once("file_a.php"); // bla bla bla The problem is that the realtive path name used for fwrite/read or any file-interative functions is NOT realative to the PHP file that defines the function, but only to the PHP file that includes the PHP file that defines the function! Essentially, the function Foo() runs differently depending on which file included FILE A. If it was in the same directory, no problem. If it was in any other directory, bad things happen. I can't use full paths either because the web server won't let me write things if i do things that way for some reason. How do i solve this? Thanks for your help.
Mathachew
Mathachew
If you could post a sample of the file structure, finding the solution will be easier.
konForce
konForce
Quote:
I can't use full paths either because the web server won't let me write things if i do things that way for some reason.

You should use an absolute path because that's exactly what you are really trying to do.

var_dump the $_SERVER array to find out your base file path. If you are on a shared host, it might be different than you expect. For example under the Ensim control panel, it might be: /home/virtual/mydomain.com/var/www/html. Then set a constant to be that path, which can be used whenever you need an absolute path.

Note that I am not encouraging against relative paths! Use them with the help of the "include_path" setting as much as possible. But here is a case where an absolute path makes the most sense.
leiavoia
leiavoia
after fiddling with it a bit, i got it working. I'm not totally sure why it wasn't working before, but it works now. I have to use full paths to get around the PHP include mess, but i put all of that info in a global define()s section. So now i can just say

Blah( SOME_PAGE, foo );

thanks for the help
Limitz
Limitz
You already found a solution, but just FYI.

The problem is that when a file is included it is interpreted from the location of the calling script. So if that file contains includes to other files, the path to that file should be either absolute or relative to the path of the calling script.

Lets say there are 3 files
./main.php
./include/1.php
./include/2.php

if 1.php is included by main.php and 1.php includes 2.php you would expect

//main.php
include "./include/1.php";

//1.php
include "2.php";

It should be:

//1.php
include "./include/2.php";

because it is interpreted from the path where main.php is.

The way i fix it is by having a php file in the same directory as main that just includes everything that is needed. I never use includes in includes

Greetings.
markr
markr
The best way of doing it is computing at runtime the absolute path of the file you want. Rather than hard-coding it, the following may be useful:

$_SERVER['DOCUMENT_ROOT'] // Gives the document rootdirname(__FILE__) // Directory name of this currently executing file.


Mark

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.