Anuj TBE
Anuj TBE

Reputation: 9790

check if file selected in field array in php

I have to update database which consists of multiple fields of file input type. name of all input fields are same.

<input type="text" name="title[]">
<input type="file" name="image[]">
<input type="text" name="title[]">
<input type="file" name="image[]">
<input type="text" name="title[]">
<input type="file" name="image[]">

Now it is not mandatory that all images are selected, there might be chances that all title are changed but only 3rd image is selected. Now i want to upload file only if image is selected else escape uploading.

this is action page:

    <?php
      $title = $_POST['title'];

$upload = 0;
    for($i=0; $i<sizeof($title); $i++)
    {
       if(!empty($_FILES['image'][$i]))
       {
           // upload file and set flag upload=1
       } else {
           // set flag upload=0
       }

       if($upload == 1)
       {
          $qry = "UPDATE <table_name> SET title='$title[$i]', image='$image' WHERE <match found>";
          // execute $qry
       } else {
          $qry = "UPDATE <table_name> SET title='$title[$i]' WHERE <match found>";
          // execute $qry
       }
    }

but every time only else statement is running event when image is selected and even when image is not selected.

Update 2 : Result of print_r($_FILES);

Array
(
    [image] => Array
        (
            [name] => Array
                (
                    [0] => 
                    [1] => Selection_238.png
                    [2] => 
                )
            [type] => Array
                (
                    [0] => 
                    [1] => image/png
                    [2] => 
                )
            [tmp_name] => Array
                (
                    [0] => 
                    [1] => /tmp/phpqSB0Jn
                    [2] => 
                )
            [error] => Array
                (
                    [0] => 4
                    [1] => 0
                    [2] => 4
                )
            [size] => Array
                (
                    [0] => 0
                    [1] => 72259
                    [2] => 0
                )
        )
)

Selected Image in 2nd input field

Upvotes: 2

Views: 796

Answers (1)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72299

First of all you need to change = to == in your code if($upload = 1). because = is assignment operator not comparisonoperator.

Check it and may be your problem is solved. Otherwise please check below possible solution

1.

<?php
  $title = $_POST['title'];

for($i=0; $i<sizeof($title); $i++)
{
   if(!empty($_FILES['image']['name'][$i]))
   {
       // check if file uploaded then run below query
        $qry = "UPDATE <table_name> SET title='$title[$i]', image='$image' WHERE <match found>"; // you can apply if else here based on move_uploaded_file output       
   } else {
       // set flag upload=0
        $qry = "UPDATE <table_name> SET title='$title[$i]' WHERE <match found>";
   }
}
?>

2.

<?php
  $title = $_POST['title'];
    $upload = 0; // define here
for($i=0; $i<sizeof($title); $i++)
{
   if(!empty($_FILES['image']['name'][$i]))
   {
       // upload file and set flag $upload = 1
   } else {
       // set flag $upload = 0
   }

   if($upload == 1) // you need to change = (assignment) to  == (comparision)
   {
      $qry = "UPDATE <table_name> SET title='$title[$i]', image='$image' WHERE <match found>";
      // execute $qry
   } else {
      $qry = "UPDATE <table_name> SET title='$title[$i]' WHERE <match found>";
      // execute $qry
   }
}
?>

Upvotes: 1

Related Questions