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
        }
    }
?>
5
  • Possible duplicate of How to check if user uploaded a file in PHP? Commented Jan 9, 2017 at 17:29
  • Use is_uploaded_file() function to check where user has uploaded a file via HTTP POST or not. Commented Jan 9, 2017 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. Commented Jan 9, 2017 at 17:31
  • This feature of php is a deep one it makes me really crazy. Is it possible to do it iteratively? Commented Jan 9, 2017 at 17:32
  • @user Yes, it's possible using iterative way. I've given an answer below, hopefully this will resolve your issue. Commented Jan 9, 2017 at 17:47

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

13 Comments

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 How and where are you using file_get_contents() function here? Please paste your code on pastebin.com and give me it's link here.
@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. Note: Make sure that your image columns can take NULL values.
@user One minor mistake is there in the above code snippet. Please test your application with this code snippet, http://pastebin.com/SYd2Nc9s
@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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.