filamentphp / filament

A collection of beautiful full-stack components for Laravel. The perfect starting point for your next app. Using Livewire, Alpine.js and Tailwind CSS.
https://filamentphp.com
MIT License
18.08k stars 2.83k forks source link

FileUpload bug with upload some files #9702

Closed OlyaK95 closed 10 months ago

OlyaK95 commented 10 months ago

Package

filament/filament

Package Version

3.0.97

Laravel Version

10.32.1

Livewire Version

3.1.0

PHP Version

8.2.8

Problem description

An error occurs when trying to upload some files. At the beginning I thought. that's a matter of file size, but it doesn't matter. Some files are uploaded, some are not.

Resource file:

FileUpload::make("documentation")
    ->label("ZIP")
    ->disk("public")
    ->directory("documentations")
    ->visibility("public")
    ->acceptedFileTypes(["application/zip"])
    ->maxSize(49152)
    ->maxFiles(1)
    ->downloadable()
    ->disabled(
        fn() => !auth()
            ->user()
            ->isAdminstrator()
    )
    ->getUploadedFileNameForStorageUsing(
        fn(
         TemporaryUploadedFile $file
        ): string => (string) str(
         $file->getClientOriginalName()
        )->prepend(now()->timestamp . "-")
    )
    ->columnSpan([
        "sm" => 1,
        "lg" => 2,
    ]),
FileUpload::make("report")
    ->label("PDF")
    ->disk("public")
    ->directory("reports")
    ->visibility("public")
    ->acceptedFileTypes(["application/pdf"])
    ->maxSize(49152)
    ->maxFiles(1)
    ->downloadable()
    ->getUploadedFileNameForStorageUsing(
        fn(
         TemporaryUploadedFile $file
        ): string => (string) str(
         $file->getClientOriginalName()
        )->prepend(now()->timestamp . "-")
    )
    ->columnSpan([
        "sm" => 1,
        "lg" => 2,
    ]),

php.ini

; Maximum size of POST data that PHP will accept.
; Its value may be 0 to disable the limit. It is ignored if POST data reading
; is disabled through enable_post_data_reading.
; https://php.net/post-max-size
post_max_size = 2G

;;;;;;;;;;;;;;;;
; File Uploads ;
;;;;;;;;;;;;;;;;

; Whether to allow HTTP file uploads.
; https://php.net/file-uploads
file_uploads = On

; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
; https://php.net/upload-tmp-dir
;upload_tmp_dir =

; Maximum allowed size for uploaded files.
; https://php.net/upload-max-filesize
upload_max_filesize = 2G

; Maximum number of files that can be uploaded via a single request
max_file_uploads = 20

Some screenshots 2023-11-17_01-33-52 2023-11-17_01-33-08 2023-11-17_01-32-21

Expected behavior

FileUpload must upload any PDF or ZIP files.

Steps to reproduce

Copy my code to any Resource file.

Reproduction repository

https://github.com/filamentphp/demo

Relevant log output

No response

sinnbeck commented 10 months ago

Check the mime of the file locally. Here is how you do it on linux

file --mime-type -b more.pdf

if it does not say applicaiton/pdf, then filepond is doing it correctly.

OlyaK95 commented 10 months ago
file --mime-type -b more.pdf

2023-11-17_12-37-02

sinnbeck commented 10 months ago

Maybe you can provide a link to the file that does not work? I have tried with several pdf files but cannot get it to fail

What is the error message exactly? I cannot read it ;)

OlyaK95 commented 10 months ago

Maybe you can provide a link to the file that does not work? I have tried with several pdf files but cannot get it to fail

Yes, of course https://disk.yandex.ru/d/xl9FHJ-ClLjt2A

What is the error message exactly? I cannot read it ;)

1) Error during load. Tap to retry

2) File of invalid type. Waiting or application/pdf

Console error: Failed to load resource: the server responded with a status of 422 (Unprocessable Content)

danharrin commented 10 months ago

Probably missing validation rule from the config/livewire.php temporary file uploads section?

OlyaK95 commented 10 months ago

Probably missing validation rule from the config/livewire.php temporary file uploads section?

I have no config/livewire.php file. Do I need it?

danharrin commented 10 months ago

Thinking about it again, probably not

But a 422 error doesn't sound like it's coming from Filament, it sounds like something on your server is blocking PDF uploads

OlyaK95 commented 10 months ago

Thinking about it again, probably not

But a 422 error doesn't sound like it's coming from Filament, it sounds like something on your server is blocking PDF uploads

@danharrin You was right. I don't know, but by default Livewire have validation that uploaded files can be max 12 MB. https://livewire.laravel.com/docs/uploads#global-validation

I added config/livewire.php with text:

<?php

return [
    "temporary_file_upload" => [
        "rules" => "file|mimes:pdf,zip|max:49152",
    ],
];

And the problem with PDF gone.

About ZIP files I have another solution - I added more file types:

FileUpload::make("documentation")
    ->label("ZIP")
    ->disk("public")
    ->directory("documentations")
    ->visibility("public")
    ->acceptedFileTypes([
        "application/x-compressed",
        "application/x-zip-compressed",
        "application/zip",
        "multipart/x-zip",
    ])
    ->maxSize(49152)
    ->maxFiles(1)
    ->downloadable()
    ->disabled(
        fn() => !auth()
            ->user()
            ->isAdminstrator()
    )