PHP

HOW TO RESTRICT FILE TYPE & SIZE IN PHP UPLOAD

<form enctype="multipart/form-data">    <input type="file" name="upfile"                required accept=".jpg,.png,">    <input type="submit" value="Upload"> </form>

HTML FILE UPLOAD FORM

01

CHECK FILE EXTENSION $accept = ["jpg", "png"]; $ext = strtolower(pathinfo($_FILES["upfile"]["name"], PATHINFO_EXTENSION)); if (!in_array($ext, $accept)) { exit("$ext files not allowed"); }

PHP UPLOAD HANDLER (A)

02

CHECK FILE SIZE $max = 10000000; // 10 MB if ($_FILES["upfile"]["size"] > $max) { exit("Max allowed size is $max"); }   SAVE UPLOAD IF OK move_uploaded_file($_FILES["upfile"]["tmp_name"], $_FILES["upfile"]["name"]);

PHP UPLOAD HANDLER (B)

03