<?php if(isset($_POST['save'])) { //Validate image if (empty($_POST["image"])) { $imageError = ""; } else { $image = check_input($_POST["image"]); $allowed = array('jpeg','jpg', "png", "gif", "bmp", "JPEG","JPG", "PNG", "GIF", "BMP"); $ext = pathinfo($image, PATHINFO_EXTENSION); if(!in_array($ext,$allowed) ) { $imageError = "jpeg only"; } } } // Validate data function check_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } <?
<?php if(isset($_FILES['image'])){ $errors= array(); $file_name = $_FILES['image']['name']; $file_size = $_FILES['image']['size']; $file_tmp = $_FILES['image']['tmp_name']; $file_type = $_FILES['image']['type']; $file_ext=strtolower(end(explode('.',$_FILES['image']['name']))); $expensions= array("jpeg","jpg","png"); if(in_array($file_ext,$expensions)=== false){ $errors[]="extension not allowed, please choose a JPEG or PNG file."; } if($file_size > 2097152) { $errors[]='File size must be excately 2 MB'; } if(empty($errors)==true) { move_uploaded_file($file_tmp,"images/".$file_name); echo "Success"; }else{ print_r($errors); } } ?> <html> <body> <form action = "" method = "POST" enctype = "multipart/form-data"> <input type = "file" name = "image" /> <input type = "submit"/> <ul> <li>Sent file: <?php echo $_FILES['image']['name']; ?> <li>File size: <?php echo $_FILES['image']['size']; ?> <li>File type: <?php echo $_FILES['image']['type'] ?> </ul> </form> </body> </html>
function get_mime_type($file) { $mtype = false; if (function_exists('finfo_open')) { $finfo = finfo_open(FILEINFO_MIME_TYPE); $mtype = finfo_file($finfo, $file); finfo_close($finfo); } elseif (function_exists('mime_content_type')) { $mtype = mime_content_type($file); } return $mtype; }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)