Page 1 of 2
include
Posted: Wed Jun 23, 2004 12:01 pm
by pppswing
Hello,
How can I do a conditional include. I would like to include one file or another file. At first time I've simple made two includes and It was working on my windows machine but when I export my code to a unix machine I got a critical error where it fail to open the file.
Thank you

Posted: Wed Jun 23, 2004 12:06 pm
by markl999
Just one way is...
Code: Select all
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
include_once 'thewinfile.php';
} else {
include_once 'theunixfile.php';
}
Though i'm not sure why you are having problems in the first place, if you use relative paths then you should be able to include the same file across all OS's *shrug*

Posted: Wed Jun 23, 2004 12:31 pm
by pppswing
No, you dont understand what I mean, the file doesn't depends on the systems I have a file that is share between other file.
and I have in my code
include('./myconfig.php');
include('../myconfig.php');
because the file are situated in different directories. So I need something that include one of this two file. It doesn't depends on the systems. With windows I have no warning displayed but with unix I have warning. So I need to make it properly.
Posted: Wed Jun 23, 2004 12:34 pm
by feyd
how about using [php_man]file_exists[/php_man] or it's siblings?
Posted: Wed Jun 23, 2004 12:34 pm
by markl999
It's much easier to put included files inside the include_path. Then you can include 'myconfig.php' from anywhere and in any directory and it will always find it (so you don't need all that . ../.. type stuff).
See
http://php.net/set_include_path or
http://php.net/ini_set (if you arn't using PHP >= 4.3.0) on how you can set your include_path
Posted: Wed Jun 23, 2004 12:35 pm
by launchcode
You need to include ONE of the two files you mentioned - but under what condition? I.e. what causes one to be included but not the other one? Do you mean you want to check to see if the file exists and if it doesn't, you try and include the other one?
Explain more, post more code.
Posted: Wed Jun 23, 2004 12:42 pm
by pppswing
If my current directory is called admin then its ../myconfig.php else its ./myconfig.php
Posted: Wed Jun 23, 2004 12:46 pm
by markl999
Apart from using the include_path you've just answered your own question
Code: Select all
if(basename(getcwd()) == 'admin'){
//or if(basename($_SERVER['PHP_SELF']) == 'admin'){
...
} else {
...
}
Posted: Wed Jun 23, 2004 4:30 pm
by pppswing
In fact it doesnt work, Ive made a search on how I could manage this problem and I find this solution :
Code: Select all
if(ereg("/admin$",getcwd())){
And now, everything is OK, This is good to know that.
admin is the name of a directory so basename will return '' because getcwd return the current path and basename the name of the file, as there is no file, it will return ''.
Thanks for your help -

Posted: Wed Jun 23, 2004 4:48 pm
by Weirdan
ppswing wrote:
admin is the name of a directory so basename will return '' because getcwd return the current path and basename the name of the file, as there is no file, it will return ''.
Code: Select all
weirdan@office:~$ php -r 'echo basename(getcwd())."\n";';
weirdan
weirdan@office:~$
it means you're wrong,
pppswing.
Posted: Wed Jun 23, 2004 5:51 pm
by pppswing
I'm wrong if I use your machine !
So why that didn't work for me
What is your php version

OS ?
basename -- Returns filename component of path
So its weird that it returns your user directory
But if we consider that in Unix everything is a file, you're right.
perhaps my host is not using the same implementation of php
Well I'm a bit astunt

Posted: Wed Jun 23, 2004 6:10 pm
by Weirdan
ok, here is another example:
Code: Select all
X:\php>php-cli.exe -r "echo basename(getcwd());"
php
X:\php>php-cli --version
PHP 4.3.4 (cli) (built: Nov 2 2003 23:47:34)
Copyright (c) 1997-2003 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2003 Zend Technologies
Posted: Wed Jun 23, 2004 6:14 pm
by Weirdan
basename works on a string, it doesn't ever touch the filesystem. Thus there's no difference between filename and directory name as far as basename concerned.
Posted: Wed Jun 23, 2004 6:25 pm
by pppswing
You are right, but it didnt work on my webhost ! That 's why I'm astound

Posted: Wed Jun 23, 2004 6:40 pm
by feyd
maybe they have getcwd disabled for some reason..
