You mean && instead of ||. A file cannot be all of those types at the same time.
in order for !A | !B | !C | !D to be evaulated as false (the desired outcome), all four conditions must be True. Since $file_type cannot be more than one of those four strings at the same time, at least 3 of the conditions are false.
If i upload 4 images in 4 input boxes then all is fine.
But
If in 1st and 2nd input box i upload PDF files and in 3rd and 4th input box i upload IMAGE files then nothing gets uploaded.
Can it be possible to upload the 2 image files and leave the rest 2 PDF files
<?php
$conn = mysql_connect("localhost","root",'') or die(mysql_error());
mysql_select_db("test",$conn);
if(isset($_FILES['files'])){
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
$file_name = $key.$_FILES['files']['name'][$key];
$file_size =$_FILES['files']['size'][$key];
$file_tmp =$_FILES['files']['tmp_name'][$key];
$file_type=$_FILES['files']['type'][$key];
if($file_size > 2097152){
echo $file_name .' exceeds limit of 2 MB <br>';
$errors = true;
}
if ( ($file_type != "image/gif") && ($file_type != "image/jpeg") && ($file_type != "image/png") && ($file_type != "image/pjpeg") )
{
echo $file_name .'extension not allowed';
$errors = true;
}
//echo $query."<br>";
$desired_dir="multiple_images_folder";
if($errors == false){
if(is_dir($desired_dir)==false){
mkdir("$desired_dir", 0700);// Create directory if it does not exist
}
if(is_dir("$desired_dir/".$file_name)==false){
move_uploaded_file($file_tmp,"$desired_dir/".$file_name);
}else{ // rename the file if another one exist
$new_dir="$desired_dir/".$file_name.time();
rename($file_tmp,$new_dir) ;
}
$query="INSERT into upload_data (file_name,file_size,file_type) VALUES('$file_name','$file_size','$file_type'); ";
mysql_query($query);
}
else{
echo $file_name ." File type Extension not allowed <br>";
}
}
if($errors == false){
echo "Success";
}
}
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="files[]" /><br />
<input type="file" name="files[]" /><br />
<input type="file" name="files[]" /><br />
<input type="file" name="files[]" /><br />
<input type="submit" name="submit" value="upload multiple"/>
</form>
I know the IF is inside the loop, but think about this:
Start at the top of your code
File 1: Type PDF
$errors = true; (previously undefined)
If($errors = false) skipped; TRUE instead.
End Loop
File 2: Type JPEG
$errors does not get changed
If($errors = false) skipped; $errors is still TRUE.
I suggest sticking $errors = false; right as the foreach begins.