<input type =“file”/> EMPTY

时间:2013-11-22 08:41:03

标签: php

我有关于php上的文件上传的这个问题。 我总是收到这个错误消息。

  

警告:file_get_contents():文件名不能为空   第25行的C:\ xampp \ htdocs \ omf2 \ emprecords \ add8.php

这是我的第25行

$data = $con->real_escape_string(file_get_contents($_FILES['uploaded_file']['tmp_name']));

但仍然将信息保存在我的数据库中。

我想要做的是将其余记录保存在我的数据库中,即使没有选择要上传的文件也是如此。是的,记录被保存,附件字段(mediumblob)是[BLOB - 0 B]

问题:如何消除错误/警告消息? (因为一切都很好)

    <meta http-equiv="refresh" content="2;URL='emphistory.php'">
    <?php
    {
    echo "<center><font color='#AAA' size='3'><br/>Record Added!</center>";
    }
    ?>
    <?php
    $con=mysqli_connect("localhost","root","","dbomf");
    if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $result = mysqli_query($con,"SELECT * FROM valueholder");   
    $row = mysqli_fetch_array($result);

    $count = '';
    $IDNUM = $row['Val'];
    $NS = addslashes($_POST ['NS']);
    $ad = addslashes($_POST ['ad']);
    $hr = addslashes($_POST ['HR']);
    $name = $con->real_escape_string($_FILES['uploaded_file']['name']);
    $data = $con->real_escape_string(file_get_contents($_FILES['uploaded_file']['tmp_name']));

    include ('../dbconn.php');

    $query = "INSERT INTO tblemphist1 VALUES
    ('".$count."', '".$IDNUM."', '".$NS."', '".$ad."', '".$hr."', '".$data."', '".$name."')";

   $result = $db->query($query) or die($db->error);
   $db->close();

这里

 <form method="post" action="add8.php" enctype="multipart/form-data">
 <td><strong>Attachment</strong></td>
    <td>:</td>
    <td><input type="file" name="uploaded_file"></td>
    </tr>
 </form>

5 个答案:

答案 0 :(得分:0)

<input type = "file">

应该是

<input name="uploaded_file" type = "file">

表格方法也应该发布并使用enctype ='multipart / form-data

<form action="" method="post" enctype="multipart/form-data">
<input name="uploaded_file" type = "file">
</form>

同时检查

$name = ''; $data = '';
if ((is_uploaded_file($_FILES['uploaded_file']['tmp_name']) && !($_FILES['uploaded_file']['error'])) {
    $name = $con->real_escape_string($_FILES['uploaded_file']['name']);
    $data = $con->real_escape_string(@file_get_contents($_FILES['uploaded_file']['tmp_name']));
}
    include ('../dbconn.php');

    $query = "INSERT INTO tblemphist1 VALUES ('".$count."', '".$IDNUM."', '".$NS."', '".$ad."', '".$hr."', '".$data."', '".$name."')";

   $result = $db->query($query) or die($db->error);

答案 1 :(得分:0)

使用if语句。例如:

if (!empty($_FILES)) {
    $data = $con->real_escape_string(
        file_get_contents($_FILES['uploaded_file'] ['tmp_name'])
    );
}

答案 2 :(得分:0)

只需检查变量是否为空

$data = '';
if (!empty($_FILES['uploaded_file']['tmp_name'])) {
    $data = $con->real_escape_string(file_get_contents($_FILES['uploaded_file']['tmp_name']));
}

答案 3 :(得分:0)

在访问$_FILES['uploaded_file']的任何属性之前,您必须检查值$_FILES['uploaded_file']['error']。是的,检查这样的密钥是否存在是个好主意 - 就像来自用户的任何东西一样,不能保证它在请求中存在。

答案 4 :(得分:0)

如果错误不会影响您的项目,只需忽略它,然后将此代码添加到您的php顶部。

<?php ERROR_REPORTING(E_ALL & ~E_NOTICE); ?>

它将忽略并隐藏错误。 :)