0

I am trying to create a post system, where, if the user writes only text and doesn't upload an image, an error image 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 Answers1

0
<?php
while ($row = mysqli_fetch_array($result)) {
if (is_uploaded_file($_FILES['image1']['tmp_name'])) {
    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>";
    }
}
?>