viappio's avatar

Unable to retrieve the file_size for file at location

I'm trying to download a file previously uploaded to storage with:

$path=$pdf->store('documents');

The upload step was ok. The file is present in my storage. But if i try to download it by:

public function downloadFile() {
        return Storage::download(storage_path("app/".$this->pdf->{File::PATH}));
    }

I receive this:

League\Flysystem\UnableToRetrieveMetadata
Unable to retrieve the file_size for file at location:

The path i get from downloadFile() is correct. If i do "more " from the terminal, it outputs the binary. I've already did this

php artisan storage:link

but nothing changed.

I'm using Laravel 9.

1 like
9 replies
vincent15000's avatar
Level 63

Storage::download gives already the path to the storage folder, you only need to add the subdirectory folder.

Storage::download("app/".$this->pdf->{File::PATH});
5 likes
lara286950's avatar

The error message indicates that Laravel's Flysystem library is unable to retrieve the metadata for the file you're trying to download. Specifically, it's unable to retrieve the file size for the file located at the given path.

There could be several reasons why Flysystem is unable to retrieve the metadata, but some common causes include:

  • The file doesn't exist at the given path.
  • The file exists, but the web server doesn't have permission to read it.

To troubleshoot the issue, you can try the following:

Check that the file exists at the given path by running file_exists(storage_path("app/".$this->pdf->{File::PATH})) before attempting to download it. Check the permissions on the file and its parent directories to ensure that the web server has permission to read the file.

2 likes
SdlasHeras's avatar

@viappio Did you solve it? I'm having the same issue. Here is my code, it enters the IF but then throws the error.

if (Storage::disk('s3')->exists('reports/' . $report->type . '' . $report->uuid . '.xlsx')) { Log::info("file exists"); return Storage::disk('s3')->download('reports/', $report->type . '' . $report->uuid . '.xlsx'); }

Error Unable to retrieve the file_size for file at location: reports.

1 like
SdlasHeras's avatar

I have finally solved, like this. The browser will handle the file download. So I just made a redirect to the file url so the browser starts downloading.

if (Storage::disk('s3')->exists('reports/' . $report->type . '' . $report->uuid . '.xlsx')) { $url = Storage::url('reports/' . $report->type . '' . $report->uuid . '.xlsx'); return redirect($url); }

1 like
Zuby's avatar

I am having the same problem but I am using the local driver. So this solution just outputs the string of the file path. it doesn't open the file. Pls any ideas?

2 likes
Zuby's avatar

Ok. trying to figure out how to do that coveringface

Please or to participate in this conversation.