I have a form with a file field called image
, but this field is not required.
When user don't choose any file in form, the do_upload()
always return a error.
How can I check if user chosen a file before perform the upload action in my controller?
if(! empty($_FILES['myFileField'])) { // file field is not empty.. } else { // no file uploaded.. }
You can check if there is a value, and if the image is valid by doing the following: if(empty($_FILES['cover_image']['tmp_name']) || ! is_uploaded_file($_FILES['cover_image']['tmp_name'])) { // Handle no image here... }
x check if file uploaded or not using hasFile or isValid. It is very easy to check if uploaded file or image empty or not if you are using core PHP. But as we know laravel 5 provide us object of file, So we can not determine using empty(). However, we can simply do it using hasFile() and isValid() of laravel predefine.
length property in jQuery to check the file is selected or not. If element. files. length property returns 0 then the file is not selected otherwise file is selected.
Please use empty()
if (empty($_FILES['userfile']['name'])) {
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With