Jump to content

Recommended Posts

I am not sure if this is a PHP question. Generally, files and directories in Windows use backslash in their paths. Unix and others use forward slash. Correct me if I am wrong.

 

However, I wish to know how php functions(especially the file functions) interpret and handle the slashes. Is it platform independent? Sometimes, I do get errors when using the wrong slash.

 

Please advise me on this.

Link to comment
https://forums.phpfreaks.com/topic/151354-forward-slash-or-back-slash/
Share on other sites

Thanks Mark. This snippet is very neat. However, there are many values PHP_OS can take. This means I need to find out all the operating systems on how they handle slashes.

 

However, I want php to figure out the slash to use instead of hardcoding based on PHP_OS. Is there such a feature?

 

Anyone has a lot of experience with such file handling in PHP?

I have read this

 

http://sg.php.net/manual/kr/function.dirname.php

 

It states that

 

On Windows, both slash (/) and backslash (\) are used as directory separator character. In other environments, it is the forward slash (/).

 

So is it safe that I can assume that forward slash / can be handled on any OS in PHP?

the way i do it is use the predifined constant called DIRECTORY_SEPARATOR

i would redefine it to something like DS to avoid having RLY long paths lol.

 

<?php
define( 'DS'	, DIRECTORY_SEPARATOR );
define( 'BASE' 	, __FILE__ );
$fs_path_array = explode( DS, BASE );
?>

 

works on both unix and windows systems.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.