PHP Determines whether the file box has selected files

Source: Internet
Author: User
Tags file upload

When a single form

The code is as follows

<form action= "?" method= "Post" enctype= ' Multipart/form-data ' >
File Upload: <input type= "file" name= "file" id= "file"/>
<input type= "Submit" id= "send" value= "submitted"/>

Determine if the pic file box has selected a file
if (!empty ($_files[' file '] [' tmp_name '])) {
Echo ' selected file ';
}else {
echo ' Please select file ';
Ps:$_files behind the [' tmp_name '] must not forget to write, it means to be a temporary meaning

Of course this can be used to determine the first JS, the following methods:

The code is as follows

var Send=document.getelementbyid ("send");
Send.onclick=function () {
var File=document.getelementbyid ("File"). Value;
if (file.length<1) {
Alert (' Please select Picture ');
return false;

Multi-File Upload <input type= "file" Name= "UploadFile" contenteditable= "false" style= "width:80%" >

There are multiple forms in the form,

When submitting a form you need to determine that at least one input has been selected for the file.

The code is as follows

<input type= "File" Name= "UploadFile" contenteditable= "false" style= "width:80%" ><br>
<input type= "File" Name= "UploadFile" contenteditable= "false" style= "width:80%" ><br>
<input type= "File" Name= "UploadFile" contenteditable= "false" style= "width:80%" ><br>
<input type= "File" Name= "UploadFile" contenteditable= "false" style= "width:80%" ><br>
<input type= "File" Name= "UploadFile" contenteditable= "false" style= "width:80%" >

Use jquery for judgment:

The code is as follows

var fileflag = false;
$ ("Input[name= ' UploadFile ']"). each (function () {
if ($ (this). Val ()!= "") {
Fileflag = true;
return false;
if (Fileflag) {
Alert ("already has a choice of good documents");

As long as Fileflag is true, you can exit each loop without having to judge the rest of the input.

Use return False to exit the loop in each, using return true to end the current secondary loop for the next loop.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.