Trying to setup a php website on my windows os. PHP code that I got from someone has lines like
$base_path = realpath('./../').'/';
This ends up with the string like c:\abc\xyz/
What settings I need to do on windows to force it to come with /
. I read about DIRECTORY_SEPERATOR
, but there are various places I need to worry about and hence if I could have it so that the realpath comes up with /
it will be of great help to me.
Windows traditionally uses the backslash ( \ ) to separate directories in file paths. (For example, C:\Program Files\PuppetLabs .)
Escape Sequences In PHP, an escape sequence starts with a backslash \ . Escape sequences apply to double-quoted strings. A single-quoted string only uses the escape sequences for a single quote or a backslash.
You can use forward slashes ( / ) instead of backward slashes ( \ ) on Windows (as is the case with Linux® and UNIX). If you use backward-slashes, the file name on Windows might be treated as a relative-path instead of an absolute-path.
How remove forward and backward slash from string in PHP? The stripslashes() function removes backslashes added by the addslashes() function. Tip: This function can be used to clean up data retrieved from a database or from an HTML form.
A variation of Timothy's answer which uses the DIRECTORY_SEPARATOR
constant instead of a conditional to simplify the function.
function platformSlashes($path) {
return str_replace('/', DIRECTORY_SEPARATOR, $path);
}
$path = "/some/path/here";
echo platformSlashes($path);
The backslash is the directory separator on the Windows platform. But from what I understand and have experienced, when resolving paths your PHP script will still work with forward slashes. As a consequence, you could write all your code with forward slashes and not worry about it. The forward/backwardslashes are really only important if you're displaying the path to the user, like in a setup/installer script (most users of a site would have no need to know about directory structures nor care what platform the service is running on). You could create a display function that would identify the platform and replace the slashes as appropriate, and then pass the paths through this before showing them. The following is an example of what I'm suggesting, though I haven't 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);
Just do a replace on the string you have:
$base_path = realpath('./../') . '/';
$base_path_mod = str_replace('\\', '/', $base_path);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With