Page 1 of 1
Help needed on File Directory Structure
Posted: Mon Mar 17, 2008 10:17 pm
by parka
I've a submit.php in the directory
/classifieds/submit.php
/inc
index.php
Code: Select all
<?php
//submit.php
require_once('../inc/include_fns.php');
?>
When I switch the path inside require_once() to root relative, it doesn't work.
Apparently, it must have jumped even further up the hierarchy.
Code: Select all
require_once('/inc/include_fns.php');
So I'm guessing the root directory PHP is seeing is not what I think it is.
On my computer, the actual file path is
/Macintosh HD/Users/MyName/Sites/htdocs/MySite/classifieds/submit.php
I need my root directory to be "MySite".
But how can I get PHP to realise what root directory I want?
Re: Help needed on File Directory Structure
Posted: Mon Mar 17, 2008 10:54 pm
by Christopher
Check the manual for the set_include_path() function.
Re: Help needed on File Directory Structure
Posted: Tue Mar 18, 2008 12:01 am
by RobertGonzalez
You could always tell it where to find the file. Where is the file you are requiring in relation to the file that is requiring it?
Re: Help needed on File Directory Structure
Posted: Tue Mar 18, 2008 1:07 am
by parka
It's better if I give an example with the following directory
/inc/class_HTMLPrinter.php
/inc/header_code.php
index.php
/classifieds/index.php
The printHTMLPage() function is:
Code: Select all
<?php
require_once('inc/header.php');
// other processing stuff below
?>
When use this function in the root index.php, it runs fine.
It doesn't work with the index.php in classifieds folder.
So I changed the path to include a slash upfront:
Code: Select all
<?php
require_once('/inc/header.php');
// other processing stuff below
?>
I'll get
Warning: require_once(/inc/header.php) [function.require-once]: failed to open stream: No such file or directory in /Users/MyName/Sites/htdocs/MySite/classifieds/index.php on line 9
Isn't the "/" suppose to get me to the root? And from there, it'll look into the 'inc' folder?
Re: Help needed on File Directory Structure
Posted: Tue Mar 18, 2008 1:24 am
by parka
Thanks for the replies.
I'm thinking of using this code:
Code: Select all
<?php
require_once($_SERVER['DOCUMENT_ROOT'] . '/some/file/path.php');
?>
I guess this can be easily ported over to any server without problems?
Re: Help needed on File Directory Structure
Posted: Tue Mar 18, 2008 1:27 am
by Christopher
It should be:
Code: Select all
<?php
require_once('../inc/header.php');
// other processing stuff below
?>
Paths that start with '/' are absolute, meaning that they start from the top of the directory structure. Paths that do not have the initial '/' are relative. Two special directories '.' the current directory and '..' the parent directory are used. The path 'inc/header.php' is the same as './inc/header.php'.
Re: Help needed on File Directory Structure
Posted: Tue Mar 18, 2008 1:40 am
by parka
arborint wrote:It should be:
Code: Select all
<?php
require_once('../inc/header.php');
// other processing stuff below
?>
Paths that start with '/' are absolute, meaning that they start from the top of the directory structure. Paths that do not have the initial '/' are relative. Two special directories '.' the current directory and '..' the parent directory are used. The path 'inc/header.php' is the same as './inc/header.php'.
Thanks, man!
Example directory
/Macintosh HD/Users/MyName/Sites/htdocs/MySite/
I was thinking that by putting a '/' would actually be absolute to my site's folder.
So when I was putting '/inc/header.php', I was actually jumping out of my site's folder.
That would make the 'inc' folder similar in hierarchy level as 'Macintosh HD', right?
Re: Help needed on File Directory Structure
Posted: Tue Mar 18, 2008 1:56 am
by devendra-m
to get rid of those path problems use following file the file is absolutePath.php
Code: Select all
<?
# Created by: Devendra Maharjan ( devendra@wlinktech.com,devendra-m@hotmail.com,devendram@gmail.com )
/*
Instructions to use :
Place this file in a folder to make it a root.
function absolutePathServer($destination)
- Pass relative path of a file to be found to function absolutePathServer() and get absolute server path.
- If you give test/test.php as parameter. The output will be as /php/test/test.php.
- Use : to include php pages.
function absolutePathClient($destination)
- Pass relative path of a file to function absolutePathClient() and get absolute client path.
- If you give test/test.php as parameter. The output will be as http://www.test.com/test/test.php
- Use : to include any client related file. eg javascript,image,css.
*/
define(ROOT_PATH,ereg_replace("^(.*)/(.*)$","\\1",str_replace('\\','/',__FILE__)));
function traverse($root,$destination)
{
if(is_dir($root))
{
$handle = opendir($root);
while (false !==($file=readdir($handle)))
if($file != '.' && $file != '..')
{
$full_path=$root."/".$destination;
if(file_exists($full_path))
return $full_path;
$path=$root."/".$file;
if($full_path=traverse($path,$destination))
return $full_path;
}
}
}
function absolutePathServer($destination)
{
if($full_path=traverse(ROOT_PATH,$destination))
return $full_path;
else
return $destination;
}
function absolutePathClient($destination)
{
if($full_path=traverse(ROOT_PATH,$destination))
return "http://".str_replace('//','/',str_replace($_SERVER['DOCUMENT_ROOT'],$_SERVER['HTTP_HOST']."/", $full_path));
else
return $destination;
}
?>
use:
Place this file in a root folder then include this file and use a function absolutePathServer as below
Code: Select all
require("absolutePath.php");
require(absolutePathServer("folder/page.php"));
Re: Help needed on File Directory Structure
Posted: Tue Mar 18, 2008 4:25 am
by Christopher
parka wrote:So when I was putting '/inc/header.php', I was actually jumping out of my site's folder.
That would make the 'inc' folder similar in hierarchy level as 'Macintosh HD', right?
Yep.