2015-01-10 69 views
-2

我想驗證PHP中的圖像數據類型和大小,但我不知道如何做到這一點。我到目前爲止驗證文本字段,但與圖像我需要一些指導。請有人協助?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

回答

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(); 
}