Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

KHAN's avatar
Level 4

How to get properties (name, size, type) of a file retrieved from storage

Hey,

I am retrieving a folder related to an event model which contains 3 or 4 files that are already stored.

            @foreach (Storage::files($event->getDirectoryPath()) as $download)
                        <div class="row">
                            <div class="col-sm-6">
                                <p>Download Name</p>
                            </div>
                            <div class="col-sm-2">PDF</div>
                            <div class="col-sm-2">125k</div>
                            <div class="col-sm-2">
                                <a href="" class="btn btn-primary btn-block">Download</a>
                            </div>
                        </div>
                    @endforeach

How do i get the file name, size and type and eventually how do i let them download this file with a click of a button without redirecting.

at the moment $download just return the string of the file. (event_attachments/86_y01e6oqrl4n92mx/myimage.png)

When i create the file, would it be easier to store the name, size and type in the database because atm i am only storing the path?

0 likes
7 replies
KHAN's avatar
Level 4

@guc43

Doesent show how to get name of file or type or download in that doc.

guc43's avatar

You get the name of the file from your $download variable. You can grap the content by $contents = Storage::get('file.jpg');. For download checkout this doc https://laravel.com/docs/5.2/responses#file-downloads.

Maybe you should put the logic in your controller and just show the names of the files in your view. With your download link can grap the file given by the name in another controlleraction and return a downloadrespond.

i cant help you with the type.

paulowdev's avatar

@KHAN

If that's what I'm thinking, is more or less this: Configuring file system In config/filesystems.php

return [

    'default' => 'custom',  // Using custom disk

    'cloud' => 's3',


    'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],
// Create custom disk
        'custom' => [
            'driver' => 'local',
            'root' => public_path() . '/uploads',
        ],

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

        's3' => [
            'driver' => 's3',
            'key' => 'your-key',
            'secret' => 'your-secret',
            'region' => 'your-region',
            'bucket' => 'your-bucket',
        ],

    ],

];

After setting up the filesystem.php execute php artisan config:cache

Then the controller:

    public function getAllFiles()
    {
        $allFiles = Storage::disk('custom')->allFiles();
        $files = array();

        foreach ($allFiles as $file) {

            $files[] = $this->fileInfo(pathinfo(public_path() . '/uploads/' . $file));
        }

        return view('files.index', compact('files'));
    }

    public function fileInfo($filePath)
    {
        $file = array();
        $file['name'] = $filePath['filename'];
        $file['extension'] = $filePath['extension'];
        $file['size'] = filesize($filePath['dirname'] . '/' . $filePath['basename']);

        return $file;
    }

Then the view:

@foreach ($files as $file)
    <div class="row">
        <div class="col-sm-6">
            <p>{{ $file['name'] }}</p>
        </div>
        <div class="col-sm-2">{{ $file['extension'] }}</div>
        <div class="col-sm-2">{{ $file['size'] }}</div>
        <div class="col-sm-2">
            <a href="" class="btn btn-primary btn-block">Download</a>
        </div>
    </div>
@endforeach 

I hope I helped you.

jordonez's avatar

I know this is an old post but I'm leaving this here in hopes that it helps someone as google top results weren't as helpful for me. I needed file size and Storage::get($pathToFile) did not work for me as well. Hence I looked into the source code of the framework. And found the following code works as it extends the League/Flysystem Library:

Storage::disk('public')->size($pathToFile) = size in bytes

Storage::disk('public')->getMimeType($pathToFile) = give the mime type

the file name is already in the file you are passing to the function call.

P.S. I'm currently using Laravel 5.8 and $pathToFile variable is relative to the disk('public') path.

2 likes
dyoung@volume9inc.com's avatar

Super old I know but since there's still no specific method call to get the file name, the easiest way I've found is something like:

$fileName = array_reverse(explode('/', $fileFromStorage))[0];

Note quite as elegant as $fileFromStorage->name() but it'll do in a pinch.

madcar's avatar

Google brought me here so I'm leaving this for future searchers.

	use Illuminate\Support\Facades\File;

	foreach(Storage::files('path/to/files') as $file) {
		// File name with extension
		File::basename($file)
		// File name without extension
		File::name($file)
	} 
4 likes

Please or to participate in this conversation.