【问题标题】:PHP - file_get_contents(): Filename cannot be emptyPHP - file_get_contents():文件名不能为空
【发布时间】:2016-07-01 13:17:04
【问题描述】:

有人可以帮我处理这个错误吗?我不知道有什么方法或方法可以摆脱这个错误。我是 php 新手并开始学习它。有人可以给我一些想法吗?

这是错误:

这是我的 php 代码。

<?php

include_once('connection.php');

 $newsid = $_GET['news_id'];

    if(isset($_POST['esubmit'])){
        /* create a prepared statement */
        if ($stmt = mysqli_prepare($con, "SELECT * FROM news WHERE news_id = ? LIMIT 1")) {
            /* bind parameters */
            mysqli_stmt_bind_param($stmt, "s", $newsid);

            /* execute query */
            mysqli_stmt_execute($stmt);

            /* get the result set */
            $result = mysqli_stmt_get_result($stmt);

            /* fetch row from the result set */
            $row = mysqli_fetch_array($result);
        }

    }


    if(isset($_POST['update'])){

        if(isset($_FILES['image'])){
          $file=$_FILES['image']['tmp_name'];
          /* Below is the line 30 causing the error*/
          $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
          $image_name= addslashes($_FILES['image']['name']);
          move_uploaded_file($_FILES["image"]["tmp_name"],"img/" . $_FILES["image"]["name"]);
          $newsimage="img/" . $_FILES["image"]["name"];

          $title = $_POST['titles'];
          $date = $_POST['dates'];
          $content = $_POST['contents'];

          $sql ="UPDATE news SET news_title ='$title', news_date ='$date', news_content = '$content', news_image ='$newsimage' WHERE news_id = '$newsid'";
          mysqli_query($con, $sql);
          echo "oh it worked ";
        }
        else{
          $title = $_POST['titles'];
          $date = $_POST['dates'];
          $content = $_POST['contents'];
          $sql ="UPDATE news SET news_title ='$title', news_date ='$date', news_content = '$content' WHERE news_id = '$newsid'";
          mysqli_query($con, $sql);
          echo "oh it worked again ";
        }

    }
?>
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>

<?php

    if(isset($_POST['esubmit'])){
        ?>

        <form method="post" action ="edit2.php?news_id=<?php echo $row['news_id']; ?>" enctype="multipart/form-data">
            Title<input type ="text" name ="titles" value="<?php echo $row['news_title']; ?>"/><br>
            Date<input type ="text" name="dates" value="<?php echo $row['news_date']; ?>" /><br>
            Content<textarea name="contents"><?php echo $row['news_content']; ?></textarea>
            <input class="form-control" id="image" name="image" type="file" accept="image/*" onchange='AlertFilesize();'/>
            <img id="blah" src="<?php echo $row['news_image']; ?>" alt="your image" style="width:200px; height:140px;"/>

            <input type="submit" name="update" value="Update" />
        </form>

        <?php
    }

?>

<script src="js/jquery-1.12.4.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script type="text/javascript">
    function readURL(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();

            reader.onload = function (e) {
                $('#blah').attr('src', e.target.result);
            }

            reader.readAsDataURL(input.files[0]);
        }
    }

    $("#image").change(function(){
        readURL(this);
    });
    </script>
</body>
</html>

【问题讨论】:

  • $_FILES['image']['tmp_name'] 返回一个临时文件,将其回显以查看它返回的数据类型,然后就会清楚错误的含义。另外,当你不再使用$image 时,为什么还要尝试获取内容?
  • 您在第一个查询中使用mysqli_stmt_bind_param()...为什么哦,为什么不将它用于其他查询???您的其他查询只是将 POST 变量直接添加到查询字符串中,从而使您容易受到 SQL 注入攻击。
  • 您还应该在条件if(isset($_FILES['image'])){ 中检查$_FILES['image'] 数组中的length
  • 为什么要加斜线?
  • @Martin 我已经编辑了我的问题先生。请再次检查谢谢。

标签: php html


【解决方案1】:

为什么要在(临时)文件名中添加斜线?

你的第 30 行:

$image= addslashes(file_get_contents($_FILES['image']['tmp_name']));

所以要删除错误警告:

if(!empty($_FILES['image']['tmp_name']) 
     && file_exists($_FILES['image']['tmp_name'])) {
    $image= addslashes(file_get_contents($_FILES['image']['tmp_name']));
}
  • 你可以/应该用这段代码做很多其他的事情,但我不能和你详细讨论,但基本上你应该检查$_FILES['image']['error'] == 0以确保只有代码如果文件已成功上传,则运行。

  • 替换

      if(isset($_FILES['image'])){
    

带有错误检查:

   if($_FILES['image']['error'] == 0){

这意味着只有上传成功的文件才会运行IF语句内容

  • 停止添加斜线,不需要。

  • 为您的 SQL 查询使用准备好的语句。

  • Move_uploaded_file 在理想情况下应该被赋予绝对路径而不是相对路径。

  • 您是否意识到您file_get_contents 正在获取文件中的数据,而不是参考文件,而是实际的二进制文件数据。这看起来不是你在这个阶段需要做的。您的$image 值在您提供的代码中没有明确使用,正如apokryfos 正确指出的那样,您实际上是在检索到的图像文件数据中添加斜杠。这只会让您的$image 变得一团糟。

【讨论】:

  • 他没有在临时文件名中添加斜线,而是在临时文件的内容中添加斜线。为什么仍然是个谜,因为它可能会破坏图像。
  • OP 的这种行为实际上 more 令人困惑!哈哈
  • 它已经工作了先生,没有错误。但是我应该把 if($_FILES['image']['error'] == 0) 放在哪里?
  • @nethken 您将引用的 if 语句替换为使用错误数组反馈的语句
  • @nethken 浏览页面中的每一行代码(如此处所示)并针对每个函数询问这是做什么的? 和那么我该如何处理这段代码行的结果?并问我需要什么结果?您的问题中有很多不需要或似乎没有明确目的的代码。
【解决方案2】:

如果其他人仍然有这个问题。那为我修好了。 将您的 php.ini 文件更改为

; Maximum allowed size for uploaded files.
upload_max_filesize = 40M

; Must be greater than or equal to upload_max_filesize
post_max_size = 40M

说明:php.ini 文件的默认设置是

; Maximum allowed size for uploaded files.
upload_max_filesize = 2M

这就是为什么您不能上传大于 2 MB 的图片的原因。 对于 Windows,您只需在底部菜单栏中的搜索栏中搜索 php.ini。然后使用编辑器编辑属性。

【讨论】:

    【解决方案3】:

    对于file_get_contents($_FILES['image']['tmp_name'])给你一个cannot be empty的错误表示它没有文件或在代码的 html 部分中没有附加任何内容。因此,首先检查您是否通过回显值 file_get_contents($_FILES['image']['tmp_name']) 来附加任何内容。

    【讨论】:

      【解决方案4】:
      <?php 
      ini_set( 'display_errors', 0 );
      error_reporting( E_ALL );
      ?>
      

      【讨论】:

      • 你能解释一下这段代码的作用吗?它如何解决给定的错误消息?
      猜你喜欢
      • 1970-01-01
      • 2021-07-26
      • 1970-01-01
      • 2014-01-03
      • 1970-01-01
      • 2021-09-06
      • 2021-09-08
      • 2014-01-31
      • 1970-01-01
      相关资源
      最近更新 更多