Laravel 5 在没有 SSH 的共享主机上:如何将上传的文件直接存储到 public_html 文件夹?

Laravel 5 on Shared Hosting with No SSH: How to store uploaded file directly to public_html folder?

我的结构是这样的:

/laravelfiles
/public_html
--/demo
----/index.php (and other files inside public folder)

我已将 index.php 更改为访问 /laravelfiles,一切正常。

我的配置文件系统:

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

'public' => [
    'driver' => 'local',
    'root' => public_path(),
    'url' => env('APP_URL').'/storage',
    'visibility' => 'public',
],

当我尝试存储文件时:

Storage::disk('public')->put($folder.'/'.$file_name, $file_data);

它存储在 laravelfiles/public 文件夹中。因为它是分开的,所以文件无法访问。

我想要的是将文件存储在 /public_html/demo 文件夹中(与 index.php 所在的文件夹相同)。

我该怎么做?

您必须通过在 AppServiceProvider.php

中添加这行代码,将应用程序中的 public 路径绑定并更改为 public_html
class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {    
          // uncomment this after deployment to actual server
        $this->app->bind('path.public', function() {
            return base_path('public_html');
        });

    }

}

尝试使用第三方存储,例如 Amazon S3 或 Ms Azure。他们也有 basic/free 计划。

尝试添加您的 config/filesystems.php

'custom' => [
        'driver' => 'custom',
        'root'   => '../path/to/your/new/storage/folder',
    ],

然后

Storage::disk('custom')->put($folder.'/'.$file_name, $file_data);