livewire: zip file fails image validation and crashes with temporaryUrl()
Description
When you upload a file that fails validation the file prop is set and you can call temporaryUrl() on it which fails.
E.g. say my validation is 'image|size:1024' then uploading a 20kb zip-file will not fail on image validation but because of temporaryUrl() isn’t supported.
ErrorException: This driver does not support creating temporary URLs. (View: /var/www/resources/views/livewire/avatar.blade.php) in file /var/www/vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemAdapter.php on line 556
Usage
@if ($photo) <!-- this is the public file prop on the Livewire component -->
<img src="{{ $photo->temporaryUrl() }}" alt="{{ $avatarable->initials }}">
@elseif ($avatarable->avatar)
<img src="{{ $avatarable->avatar->url }}" alt="{{ $avatarable->initials }}">
@else
<span class="flex items-center justify-center w-full h-full text-3xl font-bold text-white bg-primary">
<span class="group-hover:opacity-0">{{ $avatarable->initials }}</span>
</span>
@endif
If by contract I upload a zip-file larger than 1024 then I get a proper validation error as expected.
It seems like it is the image validation that fails, but it could also be that the prop isn’t unset in case of validation.
Context
- Livewire version: [e.g. 1.2.0]
- Laravel version: [e.g. 7.15.0]
- Browser: any
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 2
- Comments: 28 (5 by maintainers)
I did this as a fix for the temporary url driver issue:
I know this might not be the best code, but this helped me fix the problem without having to add every mime type to the preview_mimes array. I placed this in the blade file. Also tried binding to the photoUpdated function which should be called after update, but couldn’t get that to work. Maybe this might help someone.
For someone who is using real-time validation with several fields, solution might be like this
Regarding to the @ryanscherler workaround, can be used laravel Validator and code might look like this
I am getting the same error even with jpeg
ErrorException : This driver does not support creating temporary URLs.
I’m here with same issue … not sure I understand resolution, though.
If I am validating file type and an invalid type is not in config preview_mimes, it throws the ErrorException: This driver does not support creating temporary URLs. If I include the invalid type in preview_mimes, it works (i.e., it fails validation). But it is not practical to include every possible invalid type in preview_mimes in order to invalidate them without an ErrorException, right?
I get similar error if i try to upload anything else than an image. Is there an way to remove the unwanted files before trying to render them?
E: Looking at the TemporaryUploadedFile class i guess it’s possible
Hi Josh thanks for pointing that out. I’ve already set those but Livewire still uploads the file first, then checks the validation rules. Is this how it’s supposed to act? I am expecting the rules to be checked before the upload of the temporary files, in this way if I’m uploading a 15MB file or an invalid file format, I don’t have to wait for the upload to finish before seeing the error. Makes sense?
Happy Holidays!
@adddz temporary files also have validation, have a look at the global validation in the docs https://laravel-livewire.com/docs/2.x/file-uploads#global-validation
Hope this helps!
For a userland issue this seems to effect a lot of users…
Mine broke when livewire updated. A clear fix would be greatly appreciated.
Same issue here having issue uploading
zipdocdocxxlsxsvetc.Please provide workaround sample if possible.