File Upload Check

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Da_Elf
Forum Commoner
Posts: 81
Joined: Mon Dec 29, 2008 12:31 pm

File Upload Check

Post by Da_Elf »

I want that when your adding a new entry that there are three files that MUST be uploaded when you click the Add submit button. The code should be simple but its not working.

Code: Select all

if(isset($_POST['add'])){
     if ((!isset ($_FILES['file1'])) || (!isset ($_FILES['file2'])) || (!isset ($_FILES['file3'])) ){	
          echo "no files were selected";
     } else {
          //other code
     }
}
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: File Upload Check

Post by Christopher »

What does the code actually do? With the code below, what is the output?

Code: Select all

if(isset($_POST['add'])){
     if ((!isset ($_FILES['file1'])) || (!isset ($_FILES['file2'])) || (!isset ($_FILES['file3'])) ){	
          echo "no files were selected";
     } else {
          echo "All files selected";
     }
} else {
     echo "Not add";
}
(#10850)
Da_Elf
Forum Commoner
Posts: 81
Joined: Mon Dec 29, 2008 12:31 pm

Re: File Upload Check

Post by Da_Elf »

i dont need the last else. if in my form i have no files selected and i click add it does the 1st else and echos "All files selected". it should echo the 1st echo of "No files selected" since none of the files were set by my use of !isset
Da_Elf
Forum Commoner
Posts: 81
Joined: Mon Dec 29, 2008 12:31 pm

Re: File Upload Check

Post by Da_Elf »

here is the full thing. you can copy paste and run it. If you have no files selected it will tell you that you have all files selected which isnt true

Code: Select all

<?php
//Add Files
if(isset($_POST['add'])){
if ((!isset ($_FILES['file1'])) || (!isset ($_FILES['file2'])) || (!isset ($_FILES['file3'])) ){	
	echo "no files were selected";
	}
else {
	echo "all files selected";
	}
}//close Add Files
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form name="filestest" action="test.php" method="post" enctype="multipart/form-data">
<input name="file1" type='file'><br />
<input name="file2" type='file'><br />
<input name="file3" type='file'><br />
<input type="submit" name="add" value="Add" class="subs" />
</form>
</body>
</html>
Da_Elf
Forum Commoner
Posts: 81
Joined: Mon Dec 29, 2008 12:31 pm

Re: File Upload Check

Post by Da_Elf »

ok i went with this

Code: Select all

if (($_FILES['file1']['name'] == '' ) || ($_FILES['file2']['name'] == '' ) || ($_FILES['file3']['name'] == '' ) ){
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: File Upload Check

Post by McInfo »

If no file was uploaded for "file1", then $_FILES['file1']['error'] == UPLOAD_ERR_NO_FILE. See Error Messages Explained.
Post Reply