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

determining os for path separator?

P: n/a
After much searching, I can't seem to find any definitive way to
determine which path separator to use. Obviously, it's ":" on nix, and
";" on windows ... so what's the best way to determine what OS I'm
running on?

I've got a functional hack in place. But ... well, it's a hack.
Essentially, if my docroot begins with a "/" I just assume it's nix. So
far, it works. But I can't imagine I'm the first person to need this.
usenet searches haven't revealed much on the topic.

--cd
Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Coder Droid wrote:
After much searching, I can't seem to find any definitive way to
determine which path separator to use. Obviously, it's ":" on nix, and
";" on windows ... so what's the best way to determine what OS I'm
running on?

I've got a functional hack in place. But ... well, it's a hack.
Essentially, if my docroot begins with a "/" I just assume it's nix. So
far, it works. But I can't imagine I'm the first person to need this.
usenet searches haven't revealed much on the topic.


There's the PHP_OS constant. It's been a while since I've done any PHP
programming on Windows so I'm not sure exactly what it'll be set to on
Windows, and a quick Google didn't quite clear that up either.

However, this should be fairly safe:

if(strpos(PHP_OS, "WIN") !== false) {
$separator = ';';
}
else{
$separator = ':';
}

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #2

P: n/a
> There's the PHP_OS constant.

Weird. In all my searching, never came across that. Of course, now that
I know it's there, I zoomed right into it in the documentation. So
obvious now...

Thanks so much for the tip.

--cd
Jul 17 '05 #3

P: n/a
Well, PHP_OS isn't quite as helpful as I thought. Like you, I can't seem
to find a definitive list of all possible OS's supported. So I'm left
with "does it contain 'win' anywhere in it?" and, if not, let's just
assume the path separator is :, which should work for everything else
(linux, aix, darwin, etc.) Except for probably OS/2, but I doubt I'm
going to have to worry about that that kind of portability for a while.

I guess what PHP really needs is a global PHP_PATH_SEPARATOR. :)

--cd
Jul 17 '05 #4

P: n/a
"Coder Droid" <co********@likethiswouldstopspam.hotmail.com> wrote in message news:<yZ*******************@fe1.texas.rr.com>...
After much searching, I can't seem to find any definitive way to
determine which path separator to use. Obviously, it's ":" on nix, and
";" on windows ... so what's the best way to determine what OS I'm
running on?


Perhaps...

<?php
echo PATH_SEPARATOR;
print_r(get_defined_constants());
?>

--
| Just another PHP saint |
Email: rrjanbiah-at-Y!com
Jul 17 '05 #5

P: n/a
Coder Droid wrote:
Well, PHP_OS isn't quite as helpful as I thought. Like you, I can't seem
to find a definitive list of all possible OS's supported. So I'm left
with "does it contain 'win' anywhere in it?" and, if not, let's just
assume the path separator is :, which should work for everything else
(linux, aix, darwin, etc.) Except for probably OS/2, but I doubt I'm
going to have to worry about that that kind of portability for a while.

I guess what PHP really needs is a global PHP_PATH_SEPARATOR. :)


You may want to peruse

http://www.php.net/manual/en/reserve...s.standard.php

(Which includes the constant "PATH_SEPARATOR")
Jul 17 '05 #6

P: n/a
> You may want to peruse

http://www.php.net/manual/en/reserve...s.standard.php

(Which includes the constant "PATH_SEPARATOR")


Well, slap my ass and call me sally. I don't know how many times I
searched for "path" "separator" and just couldn't come up with
*anything*. Seriously. I mean, I searched a lot, and I'm usually pretty
good at that. Quite flummoxed over this obvious miss!

[slinks away, but with thanks]

--cd
Jul 17 '05 #7

P: n/a
Coder Droid wrote:
You may want to peruse

http://www.php.net/manual/en/reserve...s.standard.php

(Which includes the constant "PATH_SEPARATOR")


Well, slap my ass and call me sally. I don't know how many times I
searched for "path" "separator" and just couldn't come up with
*anything*. Seriously. I mean, I searched a lot, and I'm usually pretty
good at that. Quite flummoxed over this obvious miss!


No matter how well you think you know a language there's always something
new you didn't know... wish I'd known about that constant a few years ago.

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.