Php “列”userimage“不能为null”错误仅适用于较大的图像文件

Php “列”userimage“不能为null”错误仅适用于较大的图像文件,php,html,Php,Html,我在使用图像上传表单时遇到了问题,对于大多数图像,它都可以正常工作,但是对于较大的图像文件,我会得到一个列“userimage”不能为空错误 我在php.ini文件中更改了以下值: 我使用的是一个预先准备好的声明,我只是在这里包括了一小部分 $sql = "INSERT INTO gallery (user, area, userimage, socialurl) VALUES (?, ?, ?, ?)"; if($stmt = $conn->prepare($sql)){

我在使用图像上传表单时遇到了问题,对于大多数图像,它都可以正常工作,但是对于较大的图像文件,我会得到一个列“userimage”不能为空错误

我在php.ini文件中更改了以下值:

我使用的是一个预先准备好的声明,我只是在这里包括了一小部分

$sql = "INSERT INTO gallery (user, area, userimage, socialurl) VALUES (?, ?, ?, ?)";
    if($stmt = $conn->prepare($sql)){

        $stmt->bind_param("ssss", $user, $area, $userimage, $socialurl);
我试过搜索,但找不到类似的问题

在我的代码中,我删掉了一些与问题无关的内容,以缩短问题的长度

<?php
    $error = "";
    $dangererror = "";
    $successmsg = "";
    require("config/connect.php");

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

      if(empty($_POST['user'])) {
        $error .= "User is required<br>";
      } 

      if(empty($_POST['area'])) {
        $error .= "Location is required<br>";
      }      

      if(isset($_FILES["userimage"]) && $_FILES["userimage"]["error"] == 0){
        $allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png");
        echo $_FILES["userimage"]["name"];
        $userimage = $_FILES["userimage"]["name"];
        echo $userimage;
        $filetype = $_FILES["userimage"]["type"];
        $targetDir = "uploads/";
        $targetFilePath = $targetDir . $userimage;

        $targetThumbFilePath = "uploads/thumbs/";

        if(!file_exists("uploads/" . $_FILES["userimage"]["name"])){


          // Verify file extension
          $ext = pathinfo(strtolower($userimage), PATHINFO_EXTENSION);

          if(array_key_exists($ext, $allowed)) {

            $maxsize = 5000000; 

            if($_FILES['userimage']['size'] < $maxsize) {

              if (move_uploaded_file($_FILES["userimage"]["tmp_name"], $targetFilePath)){
                $successmsg .= "<div class='alert alert-success'>Image - " . $userimage . " - Uploaded Successfully!</div>";

                $upload = 'uploads/' . $_FILES["userimage"]["name"];
                list ($width, $height, $type) = getimagesize ($upload);

                //switches content-type and calls the imagecreatefrom... function
     <!------------- cut ------->

                //switches content-type and saves thumbnail
     <!------------- cut ------->

              }

            } else {
              echo "test";
              $error .= "File Exceeds 5mb Limit<br>";
              }

          } else {
              $error .= "Invalid File Format<br>";
            } 

        } else { 
            $error .= "The Filename " . $_FILES["userimage"]["name"] . " already exists - please rename your picture before uploading<br>";
          }

      }

      if($_POST['socialpage'] != ""){

        //switches content-type and saves thumbnail
      <!------------- cut ------->

      }

      if($error) {
        $dangererror = "<div class='alert alert-danger'>";
        $dangererror .= $error;
        $dangererror .= "</div>";
      } 
        else {
        // Prepare an insert statement
        $sql = "INSERT INTO gallery (user, area, userimage, socialurl) VALUES (?, ?, ?, ?)";

        if($stmt = $conn->prepare($sql)){

            // Set parameters
            $user = $_REQUEST['user'];
            $area = $_REQUEST['area'];           

            // Bind variables to the prepared statement as parameters
            $stmt->bind_param("ssss", $user, $area, $userimage, $socialurl);

            // Attempt to execute the prepared statement
            if($stmt->execute()){
                $successmsg .= "<div class='alert alert-success'>Image Added Successfully!</div>";
            } else{
                echo "ERROR: Could not execute query: $sql. " . $conn->error;
            }
        } else{
            echo "ERROR: Could not prepare query: $sql. " . $conn->error;
        }

        // Close statement
        $stmt->close();

        // Close connection
        $conn->close();
      }
    } 
?>

您的问题是,如果出现以下情况,您没有其他条款:

因此,如果$_FILES['userimage']未设置,或者存在错误,即$_FILES[userimage][error]!=0时,$error变量仍为空字符串,因此即使尚未设置$userimage的值,也会尝试进行插入。您需要添加类似以下内容的else子句:

elseif (isset($_FILES['userimage']) {
  // must have been an error uploading i.e. $_FILES["userimage"]["error"] != 0
  $error .= "Error uploading file, error code = " . $_FILES["userimage"]["error"];
}
else {
  // no uploaded file!
  $error .= "No file uploaded!";
}

问题是,当我在php.ini文件中设置以下选项时

我将值设置得太高

If the upload_max_filesize is larger than post_max_size, you must increase post_max_size so that it is bigger than upload_max_size.

这是对我的脚本的一个很好的补充。我用不正确的php.ini值解决了这个问题,但您的回答非常有用。谢谢
elseif (isset($_FILES['userimage']) {
  // must have been an error uploading i.e. $_FILES["userimage"]["error"] != 0
  $error .= "Error uploading file, error code = " . $_FILES["userimage"]["error"];
}
else {
  // no uploaded file!
  $error .= "No file uploaded!";
}
post_max_size
upload_max_filesize
memory_limit
If the upload_max_filesize is larger than post_max_size, you must increase post_max_size so that it is bigger than upload_max_size.