• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 318
  • Last Modified:

PHP: OS Appropriate Directory Separator

I want $SystemSlash to either be / or \ depending on the operating system.

How can I make $SystemSlash be the appropriate directory separator for the operating system used?
<?php

echo $SystemSlash . 'path' . $SystemSlash . 'to' . $SystemSlash . 'my/file.php';

?>

Open in new window

0
hankknight
Asked:
hankknight
1 Solution
 
k_romychCommented:
if (strpos($_SERVER['DOCUMENT_ROOT'],'/')){$SystemSlash='/';}else{$SystemSlash='\\' ;}
0
 
Rik-LeggerCommented:
Or even easier just use the constant name DIRECTORY_SEPARATOR:

<?php

echo DIRECTORY_SEPARATOR . 'path' . DIRECTORY_SEPARATOR . 'to' . DIRECTORY_SEPARATOR . 'my/file.php';

?>

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now