前进和后退斜杠php

时间:2017-11-03 11:57:08

标签: php laravel

我有一个现有的laravel项目。我在Windows 10机器上运行它。我正在运行以下命令

cd c:/xampp/htdocs/demo/ (A file called index.php is there inside demo folder)

php -S localhost:8000

index.php文件的内容如下

<?php
 $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
 $uri = urldecode($uri);
 $paths = require __DIR__.'/bootstrap/paths.php';
 $requested = $paths['public'].$uri;
 // This file allows us to emulate Apache's "mod_rewrite" functionality from the
 // built-in PHP web server. This provides a convenient way to test a Laravel
 // application without having installed a "real" web server software here.
 if ($uri !== '/' and file_exists($requested))  
  {
    return false;
  }
  require_once $paths['public'].'/index.php';
?>

现在问题在于 __ DIR __ 。当我试图打印路径时,它是向前和向后斜线的混合。

例如($ required)

C:\xampp\htdocs\demo\bootstrap/../public/ 

这不会让应用运行并抛出内部服务器错误(500)

项目中的许多地方都有 __ DIR __ 。我无法在一个地方更换它。

任何人都可以提供帮助。

2 个答案:

答案 0 :(得分:1)

在获得混合字符串

后,您可以将其中一个斜杠替换为另一个斜杠

示例:

$requested = $paths['public'].$uri;
$requested = str_replace("\\", "/", $requested);

这会将所有\替换为/

答案 1 :(得分:0)

您可以使用real_path() http://php.net/manual/en/function.realpath.php

进行游戏

另一个解决方案是不要对斜杠或反斜杠进行硬编码,而是使用DIRECTORY_SEPARATOR。无论您在何处运行代码,它都将输出特定于操作系统的路径。

function file_build_path(...$segments) {
    return join(DIRECTORY_SEPARATOR, $segments);
}

file_build_path(__DIR__, 'public', 'index.php');