0

I have 7 file input fields namely:

<input type="file" accept="image/*" name="imgreq1">
<input type="file" accept="image/*" name="imgreq2">
<input type="file" accept="image/*" name="imgreq3">
<input type="file" accept="image/*" name="imgreq4">
<input type="file" accept="image/*" name="imgreq5">
<input type="file" accept="image/*" name="imgreq6">
<input type="file" accept="image/*" name="imgreq7">

How can I check if the other input fields are empty if I for example uploaded a file in the first and the second input fields. Then submit the form leaving the other fields empty?

Update:

<?php
    if(isset($_POST['sumit'])){
        $count = count($_FILES);
        for($i = 1; $i <= $count; ++$i){
            if(is_uploaded_file($_FILES['imgreq'.$i]['tmp_name']) || !file_exists($_FILES['imgreq'.$i]['tmp_name'])){
                echo "$i";
                // your code
            }else{
                //to  retrieve user_id to stored in the request table in the database
                $query = "SELECT * FROM dummyclients_tbl WHERE user_id = '".$_SESSION['user']."'";
                if (!$result = mysql_query($query)) {
                    exit(mysql_error());
                }
                if(mysql_num_rows($result) > 0){
                    while($row = mysql_fetch_assoc($result)){
                        $sid= ''.$row['user_id'].'';
                        $coll=''.$row['college'].'';
                        $stat="Pending";

                        //$query="SELECT document_name FROM document_tbl WHERE document_id = '$passed_id'";
                        //$dn=mysql_query($query);
                        //$getname=mysql_fetch_assoc($dn);
                        //var_dump($getname);


                        //to analyze the contents of the image selected in filebrowser1
                        $image1=addslashes($_FILES['imgreq1']['tmp_name']);
                        $name1=addslashes($_FILES['imgreq1']['name']);
                        $image1=file_get_contents($image1);
                        $image1=base64_encode($image1);


                        //to analyze the contents of the image selected in filebrowser2
                        $image2=addslashes($_FILES['imgreq2']['tmp_name']);
                        $name2=addslashes($_FILES['imgreq2']['name']);
                        $image2=file_get_contents($image2);
                        $image2=base64_encode($image2);


                        //to analyze the contents of the image selected in filebrowser3
                        $image3=addslashes($_FILES['imgreq3']['tmp_name']);
                        $name3=addslashes($_FILES['imgreq3']['name']);
                        $image3=file_get_contents($image3);
                        $image3=base64_encode($image3);


                        //to analyze the contents of the image selected in filebrowser4
                        $image4=addslashes($_FILES['imgreq4']['tmp_name']);
                        $name4=addslashes($_FILES['imgreq4']['name']);
                        $image4=file_get_contents($image4);
                        $image4=base64_encode($image4);


                        //to analyze the contents of the image selected in filebrowser5
                        $image5=addslashes($_FILES['imgreq5']['tmp_name']);
                        $name5=addslashes($_FILES['imgreq5']['name']);
                        $image5=file_get_contents($image5);
                        $image5=base64_encode($image5);


                        //to analyze the contents of the image selected in filebrowser6
                        $image6=addslashes($_FILES['imgreq6']['tmp_name']);
                        $name6=addslashes($_FILES['imgreq6']['name']);
                        $image6=file_get_contents($image6);
                        $image6=base64_encode($image6);


                        //to analyze the contents of the image selected in filebrowser7
                        $image7=addslashes($_FILES['imgreq7']['tmp_name']);
                        $name7=addslashes($_FILES['imgreq7']['name']);
                        $image7=file_get_contents($image7);
                        $image7=base64_encode($image7);

                        //function nga defined sa dalum para i insert ang uploaded files sa databasess
                        saveimage($sid,$passed_id,$image1,$image2,$image3,$image4,$image5,$image6,$image7,$stat,$coll);

                    }
                }
            }
        }
    }      

    function saveimage($sid,$passed_id,$image1,$image2,$image3,$image4,$image5,$image6,$image7,$stat,$coll){
        $con=mysql_connect("localhost","root","");
        mysql_select_db("dummy",$con);
        $qry="INSERT INTO request_tbl (user_id,document_id,imgreq1,imgreq2,imgreq3,imgreq4,imgreq5,imgreq6,imgreq7,request_status,college) VALUES ('$sid','$passed_id','$image1','$image2','$image3','$image4','$image5','$image6','$image7','$stat','$coll')";
        $result=mysql_query($qry,$con);
        if($result){
            ?>
                <script>alert('Requirements Successfully Submitted!');</script>

            <?php

        }else{
            ?>
                <script>alert('Error while submitting form!');</script>
            <?php
        }
    }
?>
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
user
  • 45
  • 1
  • 8
  • Possible duplicate of [How to check if user uploaded a file in PHP?](http://stackoverflow.com/questions/946418/how-to-check-if-user-uploaded-a-file-in-php) – Alex Shesterov Jan 09 '17 at 17:29
  • Use [`is_uploaded_file()`](http://php.net/manual/en/function.is-uploaded-file.php) function to check where user has uploaded a file via HTTP POST or not. – Rajdeep Paul Jan 09 '17 at 17:29
  • foreach() Loop is not possible here as you use all file upload input name different, so i think as Rajdeep Paul suggest you has o check is)uploaded_file() for each file input. – php-coder Jan 09 '17 at 17:31
  • This feature of php is a deep one it makes me really crazy. Is it possible to do it iteratively? – user Jan 09 '17 at 17:32
  • @user Yes, it's possible using iterative way. I've given an answer below, hopefully this will resolve your issue. – Rajdeep Paul Jan 09 '17 at 17:47

1 Answers1

1

From OP's comment,

But how can you do it using for each loop this feature of php is a deep one ...

Use a simple for loop, in conjunction with is_uploaded_file() function, to check whether user has uploaded a file via HTTP POST or not, like this:

$count = count($_FILES);
for($i = 1; $i <= $count; ++$i){
    if(is_uploaded_file($_FILES['imgreq'.$i]['tmp_name'])){
        // user has uploaded a file
    }
}

Update:

Based on the below discussion with OP, the complete solution would be like this:

<?php
    if(isset($_POST['sumit'])){
        $count = count($_FILES);
        $query = "SELECT * FROM dummyclients_tbl WHERE user_id = '".$_SESSION['user']."'";
        if (!$result = mysql_query($query)) {
            exit(mysql_error());
        }
        if(mysql_num_rows($result)){
            $row = mysql_fetch_assoc($result);
            $sid = $row['user_id'];
            $coll =$row['college'];

            $query = "INSERT INTO request_tbl (user_id,document_id,imgreq1,imgreq2,imgreq3,imgreq4,imgreq5,imgreq6,imgreq7,request_status,college) VALUES ('$sid','$passed_id'";
            for($i = 1; $i <= $count; ++$i){
                if(is_uploaded_file($_FILES['imgreq'.$i]['tmp_name']) && $_FILES['imgreq'.$i]['size']){
                    $query .= ",'" . base64_encode(file_get_contents(addslashes($_FILES['imgreq'.$i]['tmp_name']))) . "'";
                }else{
                    $query .= ",NULL";
                }
            }
            $query .= ",'$stat','$coll')";
            saveimage($query);
        }
    }      

    function saveimage($qry){
        $con = new mysqli("localhost", "root", "", "dummy");
        $result=mysqli_query($con, $qry);
        if($result){
             ?>
                <script>alert('Requirements Successfully Submitted!');</script>
            <?php
        }else{
            ?>
                <script>alert('Error while submitting form!');</script>
            <?php
        }
    }
?>

As a sidenote, learn about prepared statement as it prevents your query from any kind of SQL injection attacks. Here's a good read on how you can prevent SQL injection in PHP.

Community
  • 1
  • 1
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
  • Why is it giving an error like `file_get_contents(): Filename cannot be empty in D:\xampp\htdocs\PHPv2.0\Clients\submitrequest.php on line 654` when I click submit? Is there a way to Ignore the empty fields to submit form? – user Jan 09 '17 at 17:59
  • @user How and where are you using `file_get_contents()` function here? Please paste your code on [pastebin.com](http://pastebin.com/index.php) and give me it's link here. – Rajdeep Paul Jan 09 '17 at 18:03
  • did you just anticipate that `file_get_contents` throws that kind of error? that's cool. – user Jan 09 '17 at 18:47
  • 1
    @user You're using `file_exists()` function in the wrong way, `is_uploaded_file()` function is enough to check whether a file has been uploaded or not. Please test your application with this code snippet, [http://pastebin.com/TzR4ygnh](http://pastebin.com/TzR4ygnh). **Note:** Make sure that your image columns can take `NULL` values. – Rajdeep Paul Jan 09 '17 at 18:50
  • 1
    @user One minor mistake is there in the [above code snippet](http://stackoverflow.com/questions/41553558/check-if-file-input-field-is-empty-using-foreach-loop#comment70314909_41553804). Please test your application with this code snippet, [http://pastebin.com/SYd2Nc9s](http://pastebin.com/SYd2Nc9s) – Rajdeep Paul Jan 09 '17 at 18:58
  • Thanks +1 for adjusting according to my way of programming. But it throws an error, it says; `Mysql has gone away` is this something fatal? or can be easilydebugged? – user Jan 09 '17 at 19:05
  • 1
    @user Probably because you're using older PHP version. Please know that `mysql_*` functions are deprecated as of PHP 5.5 and are removed altogether in PHP 7.0. Use `MySQLi` or `PDO` instead. Just for the debugging purpose, change your `saveimage()` function in the following way, [http://pastebin.com/EmwL5XZL](http://pastebin.com/EmwL5XZL) – Rajdeep Paul Jan 09 '17 at 19:10
  • thanks again. Now, it worked and just as you said I've set my table columns to accept `NULL` values. And also thanks for that good advice I am already studying PDO, it's just that I have no choice right now but to make this work; and it's all thanks to you. Godspeed. **Edit:** P.S please edit your answer sir so that the future readers might find it useful once again thanks :D – user Jan 09 '17 at 19:16
  • @user You're welcome! :-) I've updated my answer based on the above discussion. I'd recommend that you should also update your question as well, so that both the question and the answer could be on the same page. – Rajdeep Paul Jan 09 '17 at 19:33
  • @user Never mind, I've updated the question as well. I hope that's okay. ;-) – Rajdeep Paul Jan 09 '17 at 19:43
  • Thanks @Rajdeep Paul however when I restarted the computer and ran it again I got the `mysql has gone away` error again. – user Jan 10 '17 at 14:25
  • @user What's the exact error message you're getting? – Rajdeep Paul Jan 10 '17 at 17:54
  • it says "mysql has gone away" . In my intuition; maybe it is because of the large data which is being submitted in the form. Now it throws the "error while submitting form" which is from the program itself. Sorry for not replying immediately sir. I was kind of busy. – user Jan 25 '17 at 03:04