Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if specific input file is empty

Tags:

php

In my form I have 3 input fields for file upload:

<input type=file name="cover_image"> <input type=file name="image1"> <input type=file name="image2"> 

How can I check if cover_image is empty - no file is put for upload?

like image 393
Sasha Avatar asked Jan 22 '13 12:01

Sasha


People also ask

How do I check if an input type is empty?

Given an HTML document containing input element and the task is to check whether an input element is empty or not with the help of JavaScript. Approach 1: Use element. files. length property to check file is selected or not.

How check input field is empty or not in jQuery?

To check if the input text box is empty using jQuery, you can use the . val() method. It returns the value of a form element and undefined on an empty collection.


2 Answers

You can check by using the size field on the $_FILES array like so:

if ($_FILES['cover_image']['size'] == 0 && $_FILES['cover_image']['error'] == 0) {     // cover_image is empty (and not an error) } 

(I also check error here because it may be 0 if something went wrong. I wouldn't use name for this check since that can be overridden)

like image 192
Rudi Visser Avatar answered Oct 04 '22 21:10

Rudi Visser


Method 1

if($_FILES['cover_image']['name'] == "") { // No file was selected for upload, your (re)action goes here } 

Method 2

if($_FILES['cover_image']['size'] == 0) { // No file was selected for upload, your (re)action goes here } 
like image 20
Techie Avatar answered Oct 04 '22 21:10

Techie