简体   繁体   中英

Accessing files from a folder outside the root folder laravel

I have a project with an admin and client side. I have decided to separate the two, something am starting to regret, but I've gotten too far in to go back and merge them. Anyway, the admin part is basically for uploading stuff into the database, which are then visible to clients. The uploaded files are saved in a folder independent of the admin and client root folders.

I have no issue with uploading the files, the problem comes in when trying to display the uploaded files, which brings me to my question. How can I access files saved in a folder outside the root folder?

Folder Structure

  1. Admin Folder
  2. Folder in which uploaded files are saved (named 'common')
  3. Client Folder

Filesystem.php (Client Side)

<?php

return [

/*
|--------------------------------------------------------------------------
| 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 script retreiving the files

 $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);

Assuming your filesystem hierarchy is something like:

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

Then, to get to /path-to-projects/common , you could setup your disk like:

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

The base_path() function will get you the absolute path to your project (eg /path-to-projects/client for your client project). The parameter passed in will be added to that absolute path.

So base_path('../common') would get you /path-to-projects/client/../common , which resolves to /path-to-projects/common .

Little reminder: Wa have this file path:

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

If you use the PHP file you are in the Efolder. You have to back in the path (ParentFolder)

  • for call this image in the PHP file you must use ../Ifolder/image.png (.. to go back)

I finally solved it, thanks to Patricus's suggestion of symlinking the common directory as a new directory under the public folder for each project. Like so mklink /D "C:\wamp64\www\ngugi\get.it.here.shop_\public\common" "C:\wamp64\www\ngugi\common"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM