Search code examples
laravelpathlaravel-9laravel-filesystem

Laravel - storage_path function doubles path


I'm trying to get a path to a file in my storage folder. Although the storage_path() function duplicates the given path.

My code

$firstFile = 'app/public/temp/' . $timestamp . '/' . $tempName . '1.pdf';
$secondFile = 'app/public/temp/' . $timestamp . '/' . $tempName . '2.pdf';
$path1 = storage_path($firstFile);
$path2 = storage_path($secondFile);
dd($path1, $path2);

My result:

"/var/www/html/storage/app/public/temp/public/temp/1678441063/tempName1.pdf/public/temp/1678441063/tempName2.pdf1.pdf"
"/var/www/html/storage/app/public/temp/public/temp/1678441063/tempName1.pdf/public/temp/1678441063/tempName2.pdf2.pdf"

Wanted result:

"/var/www/html/storage/app/public/temp/1678441063/tempName1.pdf"
"/var/www/html/storage/app/public/temp/1678441063/tempName2.pdf"

My filesystem.php (file shortened for only relevant code)

<?php

return [
    'default' => env('FILESYSTEM_DISK', 'local'),

    'disks' => [

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

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

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

];

I have tried removing app/public from $firstFile but that leaves me with the same result.

I also ran the php artisan storage:link command.

I use php 8.1 and Laravel 9.19


Solution

  • Fixed thanks to @KhangTran's comment.

    My $timestamp and $tempName contained a path instead of what they should contain.