PHP如何检测文件是否由用户上传? [英] How to detect whether file is uploaded by the user or not in PHP?

查看:41
本文介绍了PHP如何检测文件是否由用户上传?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个帖子系统,如果用户只写文本而不上传图像,则不应出现错误图像(提交后仅应出现纯文本).

所以我必须检查图像是否上传.

下面的代码只发布文本.

 while ($row = mysqli_fetch_array($result)) {如果 (($_FILES['image1']['tmp_name'])==1){echo "
";echo "

";echo "

";echo "<p>".$row['image_text1']."</p>";echo "<img src='images/".$row['image1']."' width='288px'>";echo "</div>";echo "</div>";}别的{echo "
";echo "

";echo "

";echo "<p>".$row['image_text1']."</p>";echo "</div>";echo "</div>";}}

PS: 我已经将 file_exists() 函数和 is_uploaded_file() 函数用于 $_FILES['image1']['tmp_name'] 和 $_FILES['image1']['name'],尝试了很多其他方法,但都无济于事.

希望这次没有人结束这个问题,拜托了.

解决方案

";echo "

";echo "

";echo "<p>".$row['image_text1']."</p>";echo "<img src='images/".$row['image1']."' width='288px'>";echo "</div>";echo "</div>";} 别的{echo "
";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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆