PHP encounters a backslash on the way to Windows

Trying to set up a php site on my windows. The PHP code I received from someone has lines like

$base_path = realpath('./../').'/'; 

This ends with a line like c:\abc\xyz/

What settings do I need to do for Windows to make it come with / . I read about DIRECTORY_SEPERATOR , but there are different places that I need to worry about, and therefore, if I could do this so that the realpath comes up with / , it would help me a lot.

+4
source share
3 answers

The backslash is a directory delimiter on the Windows platform. But from what I understood and experienced, when resolving paths, your PHP script will work with a slash. As a result, you can write all your code using slashes and not worry about it. Direct / feedback is really important only if you show the path to the user, for example, in the installer / installer script (most users of the site will not need to know about directory structures and do not care about which platform the service works for). You can create a mapping function that identifies the platform and, if necessary, replaces the slashes, and then passes the paths through it before showing them. Below is an example of what I offer, although I have not tested it.

 <?php function platformSlashes($path) { if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') { $path = str_replace('/', '\\', $path); } return $path; } $path = "/some/path/here"; echo platformSlashes($path); 
+3
source

Just replace the line you have:

 $base_path = realpath('./../') . '/'; $base_path_mod = str_replace('\\', '/', $base_path); 
+2
source

Timothy's answer option that uses the DIRECTORY_SEPARATOR constant instead of the conditional to simplify the function.

 function platformSlashes($path) { return str_replace('/', DIRECTORY_SEPARATOR, $path); } $path = "/some/path/here"; echo platformSlashes($path); 
+2
source

All Articles