php数据验证插入图像

时间:2015-01-10 12:28:37

标签: php validation insert

我正在尝试在PHP中验证图像数据类型和大小,但我无法弄清楚如何执行此操作。我到目前为止验证文本字段,但有图像我需要一些指导。请有人帮忙吗?

    $post_cat        = dataValidation($_POST['cat']);
    $post_keywords   = dataValidation($_POST['post_keywords']);
    $post_author     = dataValidation($_POST['post_author']);


    function dataValidation($cleandata) {
      $data = trim($cleandata);
      $data = stripslashes($cleandata);
      $data = htmlspecialchars($cleandata);
      return $cleandata;
}   


    $post_image =       imageValidate($_FILES['post_image'] ['name']); 
    $post_image_type  = imageValidate($_FILES['post_image'] ['type'])
    $post_image_tmp   = imageValidate($_FILES['post_image'] ['tmp_name']);  
    $post_image_size  = imageValidate($_FILES['post_image'] ['size']); 




    function imageValidate {

       // validate image size and type here 
       }

非常感谢 P

1 个答案:

答案 0 :(得分:0)

尝试这种方法

$filecheck = basename($_FILES['imagefile']['name']);
$ext = strtolower(substr($filecheck, strrpos($filecheck, '.') + 1));

if (!(($ext == "jpg" || $ext == "gif" || $ext == "png") && ($_FILES["imagefile"]["type"] == "image/jpeg" || $_FILES["imagefile"]["type"] == "image/gif" || $_FILES["imagefile"]["type"] == "image/png") && 
    ($_FILES["imagefile"]["size"] < 2120000))){
    echo "F2";
    die();
}