【问题标题】:Accessing files from a folder outside the root folder laravel从根文件夹 laravel 之外的文件夹访问文件
【发布时间】:2020-12-22 11:49:49
【问题描述】:

我有一个带有管理员和客户端的项目。我决定将两者分开,有些事情开始后悔,但我已经走得太远了,无法回去合并它们。无论如何,管理部分基本上是用于将内容上传到数据库中,然后对客户端可见。上传的文件保存在独立于管理员和客户端根文件夹的文件夹中。

我上传文件没有问题,在尝试显示上传的文件时出现问题,这让我想到了我的问题。如何访问根文件夹以外的文件夹中保存的文件?

文件夹结构

  1. 管理文件夹
  2. 保存上传文件的文件夹(名为“common”)
  3. 客户端文件夹

Filesystem.php(客户端)

<?php

返回 [

/*
|--------------------------------------------------------------------------
| Default Filesystem Disk
|--------------------------------------------------------------------------
|
| Here you may specify the default filesystem disk that should be used
| by the framework. The "local" disk, as well as a variety of cloud
| based disks are available to your application. Just store away!
|
*/

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

/*
|--------------------------------------------------------------------------
| Default Cloud Filesystem Disk
|--------------------------------------------------------------------------
|
| Many applications store files both locally and in the cloud. For this
| reason, you may specify a default "cloud" driver here. This driver
| will be bound as the Cloud disk implementation in the container.
|
*/

'cloud' => env('FILESYSTEM_CLOUD', 's3'),

/*
|--------------------------------------------------------------------------
| Filesystem Disks
|--------------------------------------------------------------------------
|
| Here you may configure as many filesystem "disks" as you wish, and you
| may even configure multiple disks of the same driver. Defaults have
| been setup for each driver as an example of the required options.
|
| Supported Drivers: "local", "ftp", "sftp", "s3"
|
*/

'disks' => [

    'local' => [
        'driver' => 'local',
        'root' => storage_path('app'),
    ],

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

    's3' => [
        'driver' => 's3',
        'key' => env('AWS_ACCESS_KEY_ID'),
        'secret' => env('AWS_SECRET_ACCESS_KEY'),
        'region' => env('AWS_DEFAULT_REGION'),
        'bucket' => env('AWS_BUCKET'),
        'url' => env('AWS_URL'),
        'endpoint' => env('AWS_ENDPOINT'),
    ],
    'common' => [
        'driver' => 'local',
        'root' => '../common/common/',
    ],

],

/*
|--------------------------------------------------------------------------
| Symbolic Links
|--------------------------------------------------------------------------
|
| Here you may configure the symbolic links that will be created when the
| `storage:link` Artisan command is executed. The array keys should be
| the locations of the links and the values should be their targets.
|
*/

'links' => [
    public_path('storage') => storage_path('app/public'),
],

];

获取文件的 php 脚本

 $img = DB::table('product_images') #Accessing file path from database
        ->where('products_id', $id)
        ->get()->first();

       
    $img_name = $img->img;
    return Storage::disk('common')->path($img_name);

【问题讨论】:

  • 你上传文件的路径是什么?
  • C:\wamp64\www\ngugi\common\common
  • 什么是“问题”?您尝试过什么解决方法?

标签: php laravel


【解决方案1】:

假设您的文件系统层次结构类似于:

/path-to-projects/admin/app
/path-to-projects/client/app
/path-to-projects/common

然后,要访问/path-to-projects/common,您可以像这样设置磁盘:

'common' => [
    'driver' => 'local',
    'root' => base_path('../common'),
],

base_path() 函数将为您提供项目的绝对路径(例如,/path-to-projects/client 用于您的客户项目)。传入的参数将被添加到该绝对路径中。

所以base_path('../common') 会得到/path-to-projects/client/../common,它会解析为/path-to-projects/common

【讨论】:

  • 谢谢你,我已经尝试过了,当我检查图像时,这是出现在浏览器&lt;img class="card-img-top" src="C:\wamp64\www\ngugi\get.it.here.shop_\../common\004.jpg" alt=""&gt; 中的内容,所以我猜从你的建议来看,路径似乎是正确的,但仍然图片不显示
  • @MichaelKiarie 您不能像这样从网络访问不在文档根目录下的文件。否则,这将是一个巨大的安全漏洞。如果您想像访问资产一样访问这些文件,则需要将公用目录符号链接为每个项目的公用文件夹下的新目录。在 Windows 上:mklink /J "/path-to-projects/client/public/common "/path-to-projects/common"。然后你会像asset('common/004.jpg')一样访问你的资产。
【解决方案2】:

小提醒: 哇有这个文件路径:

IMAGE : Afolder/Parentfolder/Ifolder/image.png
PHP : Afolder/Parentfolder/Efolder/file.php

如果您使用 PHP 文件,则您在 Efolder 中。你必须回到路径(ParentFolder)

  • 要在 PHP 文件中调用此图像,您必须使用 ../Ifolder/image.png(.. 返回)

【讨论】:

  • 谢谢你。这是我尝试过的,..\..\common,但似乎没有任何效果
【解决方案3】:

我终于解决了,这要感谢 Patricus 将公共目录符号链接为每个项目的公共文件夹下的新目录的建议。 像这样mklink /D "C:\wamp64\www\ngugi\get.it.here.shop_\public\common" "C:\wamp64\www\ngugi\common"

【讨论】:

    猜你喜欢
    • 2017-08-31
    • 1970-01-01
    • 2015-07-01
    • 2012-06-09
    • 2014-05-24
    • 1970-01-01
    • 2016-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多