简体   繁体   English

图片上的PHP验证

[英]PHP validation on image

I am having problems with the image upload part of my code, the image uploads fine, but the validation is not working properly. 我的代码中的图片上传部分出现问题,图片上传正常,但是验证无法正常进行。

My full code can be seen here: http://phpfiddle.org/main/code/a0iw-ce7p 我的完整代码可以在这里看到: http : //phpfiddle.org/main/code/a0iw-ce7p

I can also upload an image without entering the rest of the information, all information, including upload of the image should be filled before uploading. 我也可以在不输入其余信息的情况下上传图像,包括图像上传在内的所有信息都应在上传之前填写。

The PHP fiddle would not allow this part of the code in at the end: PHP小提琴最后不允许输入这部分代码:

 else {
//upload the file

if (move_uploaded_file($_FILES["file_upload"]["tmp_name"], $target_file)) {
    echo "The file ". basename( $_FILES["file_upload"]["name"]). " has been uploaded.";     

//escapes special characters in a string for use in an SQL statement(escape variables for security)
$year = mysqli_real_escape_string($con, $_POST['year']);
$description = mysqli_real_escape_string($con, $_POST['description']);
$image = mysqli_real_escape_string($con, $_POST['file_upload']);
$decade = mysqli_real_escape_string($con, $_POST['dateID']);

//insert data into database and check whether the connection and query exist and are error free
$sql = mysqli_query($con, "INSERT into details (year, description, image, dateID) VALUES ('$year', '$description', '$target_file', '$decade')") or die(mysqli_error($sql));

//redirect user to a confirmation page
header("location: admin.php");
//close the connection when done    
mysqli_close($con);
  }
      else {
    $uploadErr = "*there was an error uploading your file";     
  }     
}
}

Here is the image validation: 这是图像验证:

// specifies the directory where the file is going to be placed
$target_dir = "img/";
$uploadOk = 0;
$year = $description = $dateID = "";
$yearErr = $descriptionErr = $decadeErr = $uploadErr = "";

//when user clicks upload

if ( !empty($_POST) ) {
    // specifies the path of the file to be uploaded
   if(!empty($_FILES["file_upload"])){
          $target_file = $target_dir . basename($_FILES["file_upload"]["name"]);

          // holds the file extension of the file
          $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
          // gets the image size
          $check = getimagesize($_FILES["file_upload"]["tmp_name"]);
     }


 //if the year is left empty then show error, if not then test_input
  if (empty($_POST["year"])) {
    $yearErr = "*enter a year";
  } else {
    $year = test_input($_POST["year"]);
  }

//if the year is left empty then show error, if not then test_input
  if (empty($_POST["year"])) {
    $yearErr = "*enter a year";
  } else {
    $year = test_input($_POST["year"]);
  }

//if the description is left empty then show error 
  if (empty($_POST["description"])) {
    $descriptionErr = "*enter a description";
  } else {
    $description = test_input($_POST["description"]);
  }

// If the date has not been selected in the drop down, and the select is left at "Please Select" value = 0
     if(isset($_POST['dateID']) && $_POST['dateID'] == '0'){
        $decadeErr = "*select a decade";    
    }

 else {
    $dateID = test_input($_POST["dateID"]);
  }



// checks the pathname and image size of the file

if($check !== false) {
        //file is an image
        $uploadOk = 1;
    }

 // if not, echo to the screen that it is not an image
    else {
        $uploadErr = "*file is not an image";
    }

// Checks if file already exists

if(file_exists($target_file)) {
    $uploadErr = "*file already exists";
}

// If file type is not jpg, png, jpeg or gif then display message
$pattern = '/(jpg|png|jpeg|gif)/';
if(!preg_match($pattern,$imageFileType)) {
 $uploadErr = "*upload an appropriate image";
}

// Check if $uploadOk is set to 0
if ($uploadOk == 0) {
    $uploadErr = "*your file was not uploaded";
}


    else {
        $uploadErr = "*there was an error uploading your file";     
    }       
}

Your logic is wrong. 你的逻辑是错误的。

Non-Image Files 非图像文件

You do your check for whether it's an image via getimagesize and assign that value to $check . 您可以通过getimagesize检查是否为图像,并将该值分配给$check Then you do this: 然后您执行以下操作:

if($check !== false) {
    //file is an image
    $uploadOk = 1;
}
// if not, echo to the screen that it is not an image
else {
    $uploadErr = "*file is not an image";
}

So, if it is not an image, now you have $check === false and $uploadErr === "*file is not an image" . 因此,如果它不是图像,那么现在您有$check === false$uploadErr === "*file is not an image" $uploadOk is still 0 . $uploadOk仍为0

But then you have this block: 但是然后您有以下代码块:

// Checks if file already exists

if(file_exists($target_file)) {
    $uploadErr = "*file already exists";
}

// If file type is not jpg, png, jpeg or gif then display message
$pattern = '/(jpg|png|jpeg|gif)/';
if(!preg_match($pattern,$imageFileType)) {
    $uploadErr = "*upload an appropriate image";
}

// Check if $uploadOk is set to 0
if ($uploadOk == 0) {
    $uploadErr = "*your file was not uploaded";
}


else {
      $uploadErr = "*there was an error uploading your file";     
}

Because $uploadOk is still 0 , the last if statement results in $uploadErr === "*your file was not uploaded" . 因为$uploadOk仍为0 ,所以最后一个if语句导致$uploadErr === "*your file was not uploaded"

Duplicate Files 重复文件

You do a check for whether the files exists and you set $uploadErr appropriately. 您检查文件是否存在,并适当设置$uploadErr But from what you have posted, it looks like you go ahead and try to move the uploaded file even if $uploadErr is non-empty. 但是从您发布的内容来看,即使$uploadErr为非空,您也可以尝试移动上载的文件。 (It's a little hard to tell what you're doing because you posted your code in two distinct pieces.) (很难分辨出您在做什么,因为您将代码分两部分发布了。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM