nekooee's avatar

missing content-length in the Response Headers

Hi, my code is :

        $media = MediaPrivate::findOrFail($id);
        $path = $media->mediaUrl;
        $fullName = $media->mediaName . '.' . $media->mediaExtension;
        $fileSize = Storage::disk('local')->size($path);
        if (Storage::disk('local')->exists($path)) {
            $headers = [
                'Content-Disposition' => 'attachment; filename="' . $fullName . '"',
                'Content-Length' => $fileSize
            ];
            return response()->download(storage_path("app/$path"), $fullName, $headers);
        } else {
            abort(404);
        }

but missing content-length in the Response Headers and Chrome doesn't show the file size when downloaded. please help me. what is the problem?

response headers:

accept-ranges: bytes
cache-control: public
cache-control: public
content-disposition: attachment; filename="file.rar"
content-encoding: gzip
content-type: application/x-rar
date: Tue, 25 Apr 2023 20:25:04 GMT
last-modified: Mon, 24 Apr 2023 19:26:05 GMT
server: Apache/2
set-cookie: XSRF-TOKEN=xxx
GMT; Max-Age=7200; path=/; samesite=lax
set-cookie: flashdump_session=xxx; path=/; httponly; samesite=lax
vary: X-Inertia,Accept-Encoding,User-Agent
0 likes
7 replies
nekooee's avatar

@Muetze I added the header to the download similar to the Laravel document. Of course, he did not add content-length in the document. Since it does not be added, I tried to add it myself. But in any case, it is still not added to the output. Is this content-length removed from the header elsewhere?

nekooee's avatar

@Muetze Thank you for taking the time to solve my problem. I modified my code similarly to yours. This is my code:

public function download($locale, $id)
    {
        $media = MediaPrivate::findOrFail($id);
        $path = $media->mediaUrl;
        $fullName = $media->mediaName . '.' . $media->mediaExtension;

        if (Storage::disk('local')->exists($path)) {
            return Storage::disk('local')->download($path, $fullName);
        } else {
            abort(404);
        }
    }

I tested your code and it works fine. But my code still has the same problem. Has Header been affected elsewhere? How can I find this out?

nekooee's avatar

@Muetze I found the problem. There is only a problem on the server, the reason for which is the addition of: content-encoding: gzip I looked at the server settings, there is such a rule:

# Don't compress images and other uncompressible content
SetEnvIfNoCase Request_URI \
 \.(?:gif|jpe?g|png|rar|zip|exe|flv|swf|mov|wma|mp3|mp4|avi|mp?g)$ no-gzip dont-vary

but this rule doesn't affect on laravel download URL. I change my url like this:

https://domain.com/en/download/file.rar?expires=1682540805&id=11&user=1&signature=xxx

And again, g-zip is applied, unfortunately. Only disabling gzip completely solves the problem. Tell me if you have a solution.

nekooee's avatar

Finally, I added php to the no-gzip list and the problem went away. Apparently, Apache does not recognize these as files at all, and PHP recognizes them. And of course, with what I did, gzip is no longer applied to any of the pages that are php, which is bad. I tried to apply this rule only to a specific url that starts with download, But whatever I tried, it didn't work, it was either completely disabled or it worked on all urls.

Please or to participate in this conversation.