簡體   English   中英

從根文件夾外的文件夾訪問文件 laravel

[英]Accessing files from a folder outside the root folder laravel

我有一個帶有管理員和客戶端的項目。 我決定將兩者分開,有些事情開始后悔了,但我太過分了 go 返回並合並它們。 無論如何,管理部分基本上是用於將內容上傳到數據庫中,然后客戶可以看到這些內容。 上傳的文件保存在獨立於管理和客戶端根文件夾的文件夾中。

我上傳文件沒有問題,問題出現在嘗試顯示上傳的文件時,這讓我想到了我的問題。 如何訪問保存在根文件夾之外的文件夾中的文件?

文件夾結構

  1. 管理員文件夾
  2. 保存上傳文件的文件夾(名為“common”)
  3. 客戶端文件夾

文件系統.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);

假設您的文件系統層次結構類似於:

/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() function 將為您提供項目的絕對路徑(例如,您的客戶項目/path-to-projects/client )。 傳入的參數將添加到該絕對路徑。

所以base_path('../common')會讓你/path-to-projects/client/../common ,它解析為/path-to-projects/common

小提醒:Wa 有這個文件路徑:

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

如果您使用 PHP 文件,則您位於 Efolder 中。 您必須返回路徑(ParentFolder)

  • 要在 PHP 文件中調用此圖像,您必須使用 ../Ifolder/ ../Ifolder/image.png (.. 到 go 返回)

我終於解決了它,感謝 Patricus 的建議,將公共目錄符號鏈接為每個項目的公共文件夾下的新目錄。 像這樣mklink /D "C:\wamp64\www\ngugi\get.it.here.shop_\public\common" "C:\wamp64\www\ngugi\common"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM