无法加载公共文件夹内的文件 - Laravel 5.4

21 浏览
0 Comments

无法加载公共文件夹内的文件 - Laravel 5.4

在使用Laravel 5.4时,我遇到了一个问题。我需要加载一些位于public文件夹下的图片public/img。为了实现这一点,我在我的blade视图中尝试了以下代码行:

在这两种情况下,我得到了一个看起来(显然)正确的URL:http://localhost/public/img/boy.png。我的应用程序在这个URL上运行良好:http://localhost:8000/

问题是图片(以及同一文件夹中的其他图片)无法加载。在HTML页面上,我在控制台中得到以下错误:http://localhost/public/img/boy.png 404 (Not Found)

这怎么可能呢?实际上,如果我尝试访问http://localhost/public/img/boy.png,我会得到一个页面不存在的错误。

这个问题怎么解决?我怎样才能访问到public文件夹中的文件?我已经花了一整天的时间来解决这个问题!

非常感谢!

0
0 Comments

在Laravel 5.4中,出现了“Cannot load files inside the public folder”的问题。这个问题的原因是图片的路径指定错误,导致无法正确加载文件。

解决方法是修改图片的URL路径。在应用运行的URL中,图片应该位于http://localhost:8000/public/img/boy.png这个路径下。

但是,我们可以通过指定端口来解决这个问题。由于应用在端口8000上运行,因此所有资源的端口都应该指定为8000。

因此,正确的图片URL应该是:http://localhost:8000/img/boy.png,而不是包含public的路径。

通过这样的修改,就可以解决“Cannot load files inside the public folder”的问题了。

0
0 Comments

问题出现的原因是在Laravel 5.4中无法加载public文件夹内的文件。解决方法是使用asset函数生成png文件的URL,并调整public_path()以满足需求。

在Laravel中,可以使用asset函数来生成文件的URL。例如,使用{{ asset('img/boy.png') }}可以生成png文件的URL。

另外,可以根据这篇文章中的建议,通过更改public_path()的值来解决该问题。代码如下:

$app->bind('path.public', function() { return __DIR__; });

根据实际需求调整__DIR__

如果遇到404错误,表示文件不存在,可以检查文件名是否正确。注意文件名的大小写,boy.pngBoy.png是不同的文件名。

还可以通过访问public_path()的输出来检查问题所在。在路由中添加以下代码来查看public_path()的结果:

Route::get('testpath', function () { return public_path(); });

根据上述回答中的更新,问题的解决方法是将public_path()的结果中的"public"部分省略,例如可以通过访问http://127.0.0.1:8000/img/boy.png来加载图片。

以上就是关于(Cannot load files inside the public folder - Laravel 5.4)问题的出现原因及解决方法的整理。

0
0 Comments

问题出现的原因是无法加载public文件夹中的文件。解决方法是将以下代码添加到路由中,并使用存储文件夹而不是公共路径。为了加载txt、css和js文件,只需在代码中更改路径即可。

Route::get('/images/{filename}', function ($filename, Illuminate\Http\Request $request)
{
    $path = public_path() . "/img/$filename";
    // $path = storage_path() . "/images/$filename"; //i normally use this, as i put it in storage folder
    //add validations if you want
    if(!File::exists($path)) abort(404);
    $file = File::get($path);
    $type = File::mimeType($path);
    $response = Response::make($file, 200);
    $response->header("Content-Type", $type);
    return $response;
});

现在,当您访问`http://localhost/images/boy.png`时,您将获得`/public/img/boy.png`。要加载txt、css和js文件,只需更改路径即可。

0