Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP on windows path comes up with backward slash

Tags:

path

php

windows

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.

like image 663
user2049305 Avatar asked Feb 07 '13 04:02

user2049305


People also ask

Why is there a backslash in Windows path?

Windows traditionally uses the backslash ( \ ) to separate directories in file paths. (For example, C:\Program Files\PuppetLabs .)

What is backward slash in PHP?

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.

Is Windows path backslash or forward slash?

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?

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.


3 Answers

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);
like image 124
Erik Berkun-Drevnig Avatar answered Oct 17 '22 00:10

Erik Berkun-Drevnig


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);
like image 33
Timothy Avatar answered Oct 17 '22 00:10

Timothy


Just do a replace on the string you have:

$base_path = realpath('./../') . '/';
$base_path_mod = str_replace('\\', '/', $base_path);
like image 33
eds Avatar answered Oct 17 '22 01:10

eds