【问题标题】:laravel doesn't upload images on serverlaravel 不会在服务器上上传图片
【发布时间】:2018-07-23 12:34:53
【问题描述】:

当我在本地工作时,我可以上传我的图片,但是当我将我的网站移动到主机时,它不会在数据库中获取图片名称时上传图片,

我的filesystems.php

'default' => env('FILESYSTEM_DRIVER', 'local'),
disks' => [

        'local' => [
            'driver' => 'local',
            'root' => public_path('images/'),
        ],
....

我的上传代码示例:

if ($request->hasFile('imageOne')) {
  $imageOne = $request->file('imageOne');
  $filename = 'productone' . '-' . time() . '.' . $imageOne->getClientOriginalExtension();
  $location = public_path('images/');
  $request->file('imageOne')->move($location, $filename);
  $product->imageOne = $filename;
}

问题:

在主机中我上传了image name in database,但我的中没有图像 public/images 文件夹。

有什么想法吗?

更新

我在主机中发现了有趣的文件夹,因为我将所有 public 文件夹文件移动到服务器 public_html 现在它在我的根目录中创建了另一个文件夹 public 并且里面有 images 文件夹,包括我的所有图像目前已上传!

问题:

  1. 为什么 laravel 在我的根目录中创建了新的 public/images 文件夹而不是 使用我的public_html
  2. 如何解决?

【问题讨论】:

  • 确保您在上传文件夹中拥有正确的权限
  • @Ikong 是吗? ibb.co/iis4nx
  • 是的,很好。但是你不应该使用 move() ,因为它只是在移动而不是上传,请参阅laravel.com/docs/5.6/filesystem#file-uploads 以获得正确的功能
  • @Ikong 我更新了我的问题,请检查一下
  • 好像是目录问题,能不能也发布一下你的public/index.php?或者你可以在那里检查它是否定义了正确的目录

标签: php laravel


【解决方案1】:

已解决

我将此代码添加到我的 public_html/index.php 文件中,它现在可以工作了。

// set the public path to this directory
$app->bind('path.public', function() {
    return __DIR__;
});

希望对你有帮助。

【讨论】:

  • 您应该尝试在AppServiceProviderregister() 方法中设置该sn-p。实际上不是返回__DIR__,而是返回base_path('public_html')
  • @Tpojka 是什么意思?
  • 找到 AppServiceProvider::register() 方法并放入 $app->bind('path.public', function() { return base_path('public_html'); }); 这样您就不需要更改 index.php 文件。
【解决方案2】:

我将保存图像控制器中的公共路径更改为如下所示:

if (request()->imagefile->move(public_path('../../public_html/destination'), $imageName))
     echo "Yap";
else echo "Nop";

【讨论】:

    猜你喜欢
    • 2018-10-28
    • 1970-01-01
    • 2020-06-29
    • 2014-07-16
    • 1970-01-01
    • 1970-01-01
    • 2011-06-01
    相关资源
    最近更新 更多