【问题标题】:Laravel 5 on Shared Hosting with No SSH: How to store uploaded file directly to public_html folder?Laravel 5 在没有 SSH 的共享主机上:如何将上传的文件直接存储到 public_html 文件夹?
【发布时间】:2019-04-30 06:45:36
【问题描述】:

我的结构是这样的:

/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 相同的文件夹)。

我该怎么做?

【问题讨论】:

    标签: php laravel cpanel


    【解决方案1】:

    尝试添加您的 config/filesystems.php

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

    然后

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

    【讨论】:

    • 谢谢。驱动自定义不起作用,我使用了本地驱动,其余的都很好。
    • @RizkiHadiaturrasyid 此代码将起作用。但是您实际上需要更改项目的公共目录。 Laravel 预定义的公共目录名为 public。但在共享主机中,公共目录是 public_html。这个解决方案可能暂时解决了您的问题,但是当其他事情发生时您会遇到同样的问题。
    • @SalarBahador 我仍然保留完整的“本地”驱动程序配置,并且仅将第二行的“驱动程序”=>“自定义”更改为“驱动程序”->“本地”。所以现在有三种配置,公共的、本地的和自定义的。
    【解决方案2】:

    您必须通过在 AppServiceProvider.php 中添加这行代码来将应用程序中的公共路径绑定并更改为 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');
            });
    
        }
    
    }
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2019-06-27
        • 2021-05-02
        • 2011-10-08
        • 2021-11-02
        • 1970-01-01
        • 2017-03-25
        • 2019-08-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多