【问题标题】:Error : Undefined index: image [duplicate]错误:未定义的索引:图像 [重复]
【发布时间】:2015-08-09 14:51:49
【问题描述】:

在你说这是重复之前,我会说我尝试了我在互联网上可以找到的所有东西,但没有任何效果。我正在尝试上传图片,但它给了我这个错误:

未定义索引:图片

<?php 


$target_dir = "images/";
    $target_file = $target_dir . basename($_FILES["image"]["name"]);

    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    if(isset($_POST["submit"])) {


        $check = getimagesize($_FILES["image"]["tmp_name"]);
        if($check !== false) {
            echo "File is an image - " . $check["mime"] . ".";
            $uploadOk = 1;
        } else {
            echo "File is not an image.";
            $uploadOk = 0;
        }
    }

    if (file_exists($target_file)) {

        echo "Sorry, file already exists.";
        $uploadOk = 0;

    }

    if ($uploadOk == 0) {
      die(mysqli_error($dbc));
    echo "Sorry, your file was not uploaded.";
} else {
    if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["image"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
  }

 ?>
 <form action="SchimbareImagine.blade.php" method="POST" enctype="multipart/form-data">
<div class="form-group">
      <label>Poster:</label>
      <div class="col-lg-10">
          <input id="input-5" type="file" name = "image" >
        </div>
    </div>
</div>

<button name = "submit" type="submit" id = "submit" >Submit</button>
</form>

有谁知道我该如何解决这个问题?

【问题讨论】:

  • 在尝试使用之前,只需使用isset() 检查$_FILES['image'] 是否存在。
  • 现在它说:未定义变量:target_file
  • 你能把修改后的代码贴出来吗?

标签: php html image


【解决方案1】:

您收到此错误,因为尚未设置 $_FILES["image"] 变量/数组。

由于您已经在使用die() 方法,您可以通过以下方式阻止代码继续执行:

if(!isset($_FILES["image"])){
    echo "ERROR: no image revived";
    die();
}

试试这样:

if(isset($_FILES["image"])){
$target_dir = "images/";
    $target_file = $target_dir . basename($_FILES["image"]["name"]);

    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    if(isset($_POST["submit"])) {


        $check = getimagesize($_FILES["image"]["tmp_name"]);
        if($check !== false) {
            echo "File is an image - " . $check["mime"] . ".";
            $uploadOk = 1;
        } else {
            echo "File is not an image.";
            $uploadOk = 0;
        }
    }

    if (file_exists($target_file)) {

        echo "Sorry, file already exists.";
        $uploadOk = 0;

    }

    if ($uploadOk == 0) {
      die(mysqli_error($dbc));
    echo "Sorry, your file was not uploaded.";
    } else {
    if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["image"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
  }
}

【讨论】:

  • 嗯问题是我进入页面后出现错误,我没有提交任何东西或做任何事情。(我使用 laravel 是一个问题吗?)
  • 我已经更新了我的答案。请检查我添加到答案中的最后一行。
  • 同样的错误,没有任何改变。代码的问题似乎在这一行 $target_file = $target_dir 。 basename($_FILES["image"]["name"]);
  • 请查看我更新的答案。
  • 这很好,非常感谢。
猜你喜欢
  • 2017-01-06
  • 1970-01-01
  • 2018-04-14
  • 1970-01-01
  • 1970-01-01
  • 2012-11-14
  • 2011-12-04
  • 2018-09-11
相关资源
最近更新 更多