By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,664 Members | 1,643 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,664 IT Pros & Developers. It's quick & easy.

require_once problem

P: n/a
I hava several php files in different folders.

I tried to use relative paths. But when I call a script from another
folder that has another depth, it cannot find the files that are
embedded via require once within the included file.

sample:

fileA.php (in the root dir!)
-----
require_once 'common_inc.php';
....
-----

classes/fileB.php (in /classes)
-----
require_once '../fileA.php';
....
-----
No it trys to find the 'common_lib in the /classes folder insted of the
root dir.
Well, I tried now to specify absolute paths like
"http://127.0.0.1:8080/projectname/common_inc.php"
But it cannot be included then. Thats the browser string of the file.
Why doesn't it work?
How can I solve this problem?
It doesn't matter for me if relative or absolute paths specifications
are used. It should only work :)
thanks in advance,
Lars
Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Lars Plessmann wrote:
Well, I tried now to specify absolute paths like
"http://127.0.0.1:8080/projectname/common_inc.php"
But it cannot be included then. Thats the browser string of the file.
Why doesn't it work?


Use filesystem paths:

require_once 'C:/htdocs/common_inc.php';
require_once 'C:/htdocs/classes/fileB.php';
--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #2

P: n/a
Pedro Graca wrote:
Lars Plessmann wrote:
Well, I tried now to specify absolute paths like
"http://127.0.0.1:8080/projectname/common_inc.php"
But it cannot be included then. Thats the browser string of the file.
Why doesn't it work?

Use filesystem paths:

require_once 'C:/htdocs/common_inc.php';
require_once 'C:/htdocs/classes/fileB.php';


problem: I develop on windows and my host is unix.
how to handle this?
Jul 17 '05 #3

P: n/a
Lars Plessmann wrote:
Pedro Graca wrote:
Lars Plessmann wrote:
Well, I tried now to specify absolute paths like
"http://127.0.0.1:8080/projectname/common_inc.php"
But it cannot be included then. Thats the browser string of the file.
Why doesn't it work?

Use filesystem paths:

require_once 'C:/htdocs/common_inc.php';
require_once 'C:/htdocs/classes/fileB.php';


problem: I develop on windows and my host is unix.
how to handle this?


Maybe like this (PHP 5)?
<?php
if (stripos(PHP_OS, "win") === false) {
require_once '/htdocs/common_inc.php';
} else {
require_once 'C:/htdocs/common_inc.php';
}
?>
stripos() is available from PHP 5

PHP_OS is a core predefined constant
http://www.php.net/manual/en/reserve...tants.core.php

if you don't have PHP 5 try:

<?php
function my_stripos($haystack, $needle, $offset=0) {
return strpos(strtoupper($haystack), strtoupper($needle), $offset);
}
?>

--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #4

P: n/a

"Lars Plessmann" <La************@gmx.de> wrote in message
news:40***********************@newsread4.arcor-online.net...
I hava several php files in different folders.

I tried to use relative paths. But when I call a script from another
folder that has another depth, it cannot find the files that are
embedded via require once within the included file.

sample:

fileA.php (in the root dir!)
-----
require_once 'common_inc.php';
...
-----

classes/fileB.php (in /classes)
-----
require_once '../fileA.php';
...
-----
No it trys to find the 'common_lib in the /classes folder insted of the
root dir.
Well, I tried now to specify absolute paths like
"http://127.0.0.1:8080/projectname/common_inc.php"
But it cannot be included then. Thats the browser string of the file.
Why doesn't it work?
How can I solve this problem?
It doesn't matter for me if relative or absolute paths specifications
are used. It should only work :)
thanks in advance,
Lars


$_SERVER["DOCUMENT_ROOT"] should give you the top-level document root
("/var/www/html" or "c:\inetpub\wwwroot", say) - if your include storage is
consistent, you could trust that.

Garp
Jul 17 '05 #5

P: n/a
> I hava several php files in different folders.

I tried to use relative paths. But when I call a script from another
folder that has another depth, it cannot find the files that are
embedded via require once within the included file.

sample:

fileA.php (in the root dir!)
-----
require_once 'common_inc.php';
...
-----

classes/fileB.php (in /classes)
-----
require_once '../fileA.php';
...
-----


my idea:
-----
require_once dirname(__FILE__).'../fileA.php';
-----

dirname(__FILE__)." relative paths ". " file ";

--
CHARA=>TOMeK
Jul 17 '05 #6

P: n/a
> my idea:
-----
require_once dirname(__FILE__).'../fileA.php';
-----

dirname(__FILE__)." relative paths ". " file ";


This is good:
-----
require_once dirname(__FILE__).'/'.'../fileA.php';
-----

dirname(__FILE__).'/'." relative paths ". " file ";

Pozdrawiam,
CHARA=>TOMeK
Jul 17 '05 #7

P: n/a
"Lars Plessmann" <La************@gmx.de> wrote in message
news:40***********************@newsread4.arcor-online.net...
I hava several php files in different folders.

I tried to use relative paths. But when I call a script from another
folder that has another depth, it cannot find the files that are
embedded via require once within the included file.

sample:

fileA.php (in the root dir!)
-----
require_once 'common_inc.php';
...
-----

classes/fileB.php (in /classes)
-----
require_once '../fileA.php';
...
-----
No it trys to find the 'common_lib in the /classes folder insted of the
root dir.
What was the current directory when you tried this? What was the
include_path setting when you tried this?
Well, I tried now to specify absolute paths like
"http://127.0.0.1:8080/projectname/common_inc.php"
But it cannot be included then. Thats the browser string of the file.
Why doesn't it work?
You use file system paths, not URLS
How can I solve this problem?
It doesn't matter for me if relative or absolute paths specifications
are used. It should only work :)


We can't really say until you tell us about your include path and identify
the directory in which the script initially started.

- Virgil
Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.