0

I am trying to create a post system, where, if the user writes only text and doesn't upload an image, a blank image error should not appear (only plain text should appear after submitting).

So I have to check whether an image is uploaded or not.

The below code does the work of posting only Text.

   while ($row = mysqli_fetch_array($result)) {

    if (($_FILES['image1']['tmp_name'])==1){
    echo "<br>";
    echo "<div class='postuser'>";

    echo "<div id='img_div1' class='caption'>";
    echo "<p>".$row['image_text1']."</p>";
    echo "<img src='images/".$row['image1']."' width='288px'>";
    echo "</div>";

    echo "</div>";
    }

    else
    {
    echo "<br>";
    echo "<div class='postuser'>";

    echo "<div id='img_div1' class='caption'>";
    echo "<p>".$row['image_text1']."</p>";
    echo "</div>";

    echo "</div>";
    }
}

PS: I have used file_exists() function and is_uploaded_file() function with both $_FILES['image1']['tmp_name'] and $_FILES['image1']['name'], tried many other ways, but to no help.

Hope nobody closes the question this time, please.

Yash Pawse
  • 41
  • 10
  • 1
    Possible duplicate of [How to detect whether file is uploaded by the user or not in PHP?](https://stackoverflow.com/questions/49223289/how-to-detect-whether-file-is-uploaded-by-the-user-or-not-in-php) – CBroe Mar 15 '18 at 17:59
  • _"Hope nobody closes the question this time, please."_ - then _you_ would have to do a better job first, and not just give us the 1:1 same question again. – CBroe Mar 15 '18 at 18:00
  • 1
    What is this code even supposed to do, why would you check $_FILES in a while loop that loops over a database result? Are you maybe not actually _processing_ the upload of a file here, but are trying to output the previously entered data from the database? Well $_FILES plays no role in that _at all_. – CBroe Mar 15 '18 at 18:03
  • Sorry @CBroe, I'm new to this language, I might not be as experienced as you are. But I think the question wasn't the exact replica of some other question. Thank You for giving your time. :) – Yash Pawse Mar 15 '18 at 18:07
  • 1
    It would be a duplicate, if you were actually trying to process a file that was uploaded with the current request - how to check if the user sent a file, and whether there were any errors, is something you could read up on in those duplicates. But your problem seems to be something rather different, you seem to be in the phase of outputting previously stored data, and $_FILES doesn't play a role in that at all. – CBroe Mar 15 '18 at 18:11
  • Thank you CBroe, you are right. I removed that part and tried it again with below answered code and it worked! :) – Yash Pawse Mar 15 '18 at 18:14

1 Answers1

0

This code solved my query

   while ($row = mysqli_fetch_array($result)) {
    echo "<br>";
    echo "<div class='postuser'>";
    echo "<div id='img_div1' class='caption'>";
    echo "<p>".$row['image_text1']."</p>";
    if(!empty($row['image1']))
    {
    echo "<img src='images/".$row['image1']."' width='288px' height='250px' class='postedimg'  >";
    }

    else
    {
        echo "";
    }
  echo "</div>";
   echo "<p>".$row['sr']."</p>";

   echo "</div>";
}
?>
Yash Pawse
  • 41
  • 10