【问题标题】:upload image with filename cannot be empty上传图片文件名不能为空
【发布时间】:2016-05-23 10:14:25
【问题描述】:

我正在做一个上传图片的页面,一切都很好。但是当我不小心上传图片而没有选择任何图片时,问题就出现了。出现警告“警告:file_get_contents():文件名不能为空”。我尝试解决,但仍然出现警告。我需要添加什么吗?我应该怎么做才能删除该警告?这是我的php代码

<form name="form1" action="" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>select files</td>
<td><input type="file" name="f1"></td>
</tr>
<td><input type="submit" name="submit1" value="upload"></td>
<td><input type="submit" name="submit2" value="display"></td>
</table>
</form>

<?php
$con = mysqli_connect("localhost","root","");
mysqli_select_db($con,"image")or die(mysqli_error($con));;
if(isset($_POST["submit1"]))
{
    $image = addslashes(file_get_contents($_FILES['f1']['tmp_name']));
    $sql ="INSERT INTO images (username, image)  VALUES('$_SESSION[username]','$image')";
    $result=mysqli_query($con, $sql);

}if(isset($_POST["submit2"]))
{
    $sql = "SELECT username, image FROM images where username = '$_SESSION[username]' ";
    $result = mysqli_query($con,$sql);
    echo"<table>";
    echo "<tr>";
    while ($row=mysqli_fetch_assoc($result))
    {
        echo "<td>";
        echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image']).'" height="100" width="100"/>';
        echo "</td>";


    }
    echo "</tr>";
}

?>
?>

【问题讨论】:

  • 也发布您的表单代码!!
  • 阅读this以检查图像验证

标签: php mysqli


【解决方案1】:

使用此代码

    if(isset($_POST["submit1"]))
            {
   echo  $image=$_POST['f1'];
            if($image)
            {
             $image = addslashes(file_get_contents($_FILES['f1']['tmp_name']));
                $sql ="INSERT INTO images (username, image) VALUES     ('$_SESSION[username]','$image')";
                $result=mysqli_query($con, $sql);

            }
            else

            {

           $msg="Filename cannot be empty";
            echo "<script type='text/javascript'>alert('$msg');</script>";
            }
            }

【讨论】:

  • 现在我无法将图像上传到我的数据库。
  • 没有错误但无法上传图片,上传时会显示文件名不能为空。
  • @user3680780 我已经更改了我的代码试试这个代码并检查图像名称是否显示...
猜你喜欢
  • 1970-01-01
  • 2013-07-12
  • 1970-01-01
  • 2015-01-22
  • 2018-08-06
  • 1970-01-01
  • 2013-12-17
  • 2015-01-21
  • 2016-07-05
相关资源
最近更新 更多