Laravel的public_path()返回C:\wamp\www\而不是C:/wamp/www。

3
 $path = (public_path("images/") . $filename);

 echo $path;

C:\wamp\www\jobpost\public\images/Person.PNG //这里的斜杠有问题

我想让这个路径可以访问我的图片

C:\wamp\www\jobpost\public\images\Person.PNG //该如何实现

 (public_path("images\") . $filename); 
 //i add the slash like this error occur

echo str_replace(chr(47),chr(92),$path); 在这里使用 ord,所以不需要转义。 - JustOnUnderMillions
@JustOnUnderMillions 或者可以使用 DIRECTORY_SEPARATOR 来分隔这两个路径; - hassan
PHP几乎在任何地方都接受这两种分隔符。那么“问题从中产生”确切意味着什么? - Álvaro González
1个回答

2

您正在Windows系统中,但提供了Linux目录分隔符。

您可以直接使用Windows的分隔符,如下所示:

$path = (public_path("images\\") . $filename);

为了让代码更安全,您应该使用预定义的DIRECTORY_SEPARATOR常量,如下所示:

$path = (public_path("images") . DIRECTORY_SEPARATOR . $filename);

更新

问题在于:

(public_path("images\") . $filename);

你需要转义双引号,因为你需要用它来包含你的字符串参数。


我该如何在JSON响应中创建链接?使用return response()->file($path);,图片可以直接在Postman中显示。这意味着当用户单击此链接时,在响应中显示链接,然后显示图片。你有任何想法吗? - Mukkaram Waheed
response()->json([$path]); - hassan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接