【问题标题】:How to upload files from subdomain folder to root folder in laravel?如何将文件从子域文件夹上传到laravel中的根文件夹?
【发布时间】:2020-10-16 03:29:25
【问题描述】:

我在 public_html 文件夹中有一个作为 abc.xyz.com 的子域文件夹,其中有一个 laravel 项目。现在我正在从子域文件夹上传图像。但我想将图像存储在具有 laravel 项目的主域中,即 xyz.com。我怎样才能做到这一点?请帮忙 这是我正在使用的图片上传:

<?php

if ($request->hasFile('frontimage')) {

    $file_frontimage = $request->file('frontimage');
    $actual_filename_frontimage = $file_frontimage->getClientOriginalName();
    $filename_frontimage = time() . '_' .$actual_filename_frontimage;

    $file_frontimage->storeAs('images', $filename_frontimage, 'public');
}

【问题讨论】:

标签: laravel


【解决方案1】:

您可以指定一个绝对路径。为此,您可以在名为config/filesystems.php 的配置文件中创建一个自定义磁盘。 关键是您在子域端创建该磁盘。打开该配置文件并根据需要进行修改,如下所示:

<?php

'disks' => [

    'local' => [
        'driver' => 'local',
        'root' => storage_path('app'),
    ],
    
    // Here is your custom disk
    'parent_disk' => [
        'driver' => 'local',

        // This should be a correct absolute path, so change it with yours
        'root'   => '/home/your_username/public_html/storage/app/public',
        'visibility' => 'public',
    ],
     
    // More disks
],

接下来,您在从子域上传图像时指定磁盘名称。

<?php

if ($request->hasFile('frontimage')) {
    $file_frontimage = $request->file('frontimage');
    $actual_filename_frontimage = $file_frontimage->getClientOriginalName();
    $filename_frontimage = time() . '_' .$actual_filename_frontimage;
    
    // Notice the 3rd argument that is the disk name
    // you created in sub-domain via `config/filesystems.php` file
    $file_frontimage->storeAs('images', $filename_frontimage, 'parent_disk');
}

这可能比使用符号链接更便携。

【讨论】:

  • 谢谢先生。您的代码运行良好。但是,如果我想为同一图像创建 3 个缩略图大小,则只会保存一个缩略图图像。你能帮助我吗? $file_frontimage->storeAs('thumbnailimages', '525x350'.$filename_frontimage, 'parent_disk'); $thumbnailpath_525x350= public_path('storage/thumbnailimages/'.'525x350'.$filename_frontimage); $img_525x350= 图片::make($thumbnailpath_525x350)->resize(525,350); $img_525x350->保存($thumbnailpath_525x350);
  • 缩略图正在保存,但错误显示为“Intervention\Image\Exception\NotReadableException Image source not readable”
  • 嗯,这有点奇怪!我测试了它并且它有效。 Here is the code我用过。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-01
  • 1970-01-01
  • 2012-05-10
  • 1970-01-01
  • 2014-06-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多