错误:未定义索引:图像

时间:2015-08-09 14:51:49

标签: php html image

在你说这是重复之前,我会说我尝试了我能在互联网上找到的所有内容,但没有任何作用。我正在尝试上传图片,它给了我这个错误:

  

未定义索引:图像

<?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>

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

1 个答案:

答案 0 :(得分: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.";
    }
  }
}