";echo "<p>".$row['image_text1']."</p>";echo "<img src='images/".$row['image1']."' width='288px'>";echo "</div>";echo "</div>";} 别的{echo "
";echo "
";echo "<p>".$row['image_text1']."</p>";echo "</div>";echo "</div>";}}?>
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.
解决方案
<?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>";
}
}
?>
这篇关于PHP如何检测文件是否由用户上传?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文