Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if the file input field is empty?

Tags:

I am having a hard time using $_FILES

I want to check if file upload field is empty or not then apply a condition such that if file upload is empty then the script doesn't try uploading the file. How do I enforce this?

like image 419
Mohamed Hassan Avatar asked Apr 10 '12 21:04

Mohamed Hassan


People also ask

How do you check if an input 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 do I empty a file input?

We can clear the file input with JavaScript by setting the value property of the file input to an empty string or null .

How do you check a file is empty or not in PHP?

PHP empty() Function The empty() function checks whether a variable is empty or not. This function returns false if the variable exists and is not empty, otherwise it returns true. The following values evaluates to empty: 0.


2 Answers

if($_FILES["file"]["error"] != 0) { //stands for any kind of errors happen during the uploading }  

also there is

if($_FILES["file"]["error"] == 4) { //means there is no file uploaded } 
like image 195
Mohamed Hassan Avatar answered Oct 12 '22 00:10

Mohamed Hassan


This should work

if ( ! empty($_FILES)) {...} 
like image 41
Drazen Avatar answered Oct 12 '22 00:10

Drazen