【问题标题】:move uploaded file to root/public_html将上传的文件移动到 root/public_html
【发布时间】:2020-02-18 09:49:15
【问题描述】:

我有一个 laravel 项目并将其上传到共享主机上 我有此代码用于将上传的文件移动到文件夹: $request->file->move(public_path('images\banners'), $new_name);

我的网站是这样的: 根/ 拉拉维尔 public_html

我只想将上传的文件移动到 public_html 文件夹

public function register()
{
    //
    $this->app->bind('path.public', function() {
        return realpath(base_path().'/../public_html');
    });
}

我试试这个,但它不起作用

【问题讨论】:

    标签: laravel upload


    【解决方案1】:

    laravel 的方式是使用 FlySystem。打开config/filesystems.php 并为public_html 添加一个新条目。

    'disks' => [
    
        'public_html' => [
            'driver' => 'local',
            'root'   =>  '/full/path/to/public_html', 
        ],
    ...
    

    然后将其用作Storage::disk('public_html')->put('filename', 'contents');。有关如何将文件移动或上传到特定存储的文档还有其他方法https://laravel.com/docs/5.8/filesystem

    此外,您可以使用诸如storage_path()public_path() 等辅助函数来代替指定完整路径,具体取决于您的目录位置。

    【讨论】:

      【解决方案2】:
          $file = $request->file('image');
          $extension = $file->getClientOriginalExtension(); // getting image extension
          $filename =time().'.'.$extension;
          $path = $file->move(public_path('uploads'), $filename);
          $newPath = explode('\public\\', $path);
          $filePath = end($newPath);
      

      【讨论】:

        猜你喜欢
        • 2011-10-08
        • 2021-05-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-26
        • 2013-04-09
        • 2018-07-16
        相关资源
        最近更新 更多