I’m trying get my site to error out if the file uploaded ins’t a .jpg, .jpeg or .JPG
<?php
function error(){echo"<script>$(function(){alert('Error')});</script>"; exit();}
$fileName = $_FILES['image']['name'];
$ext = substr(strrchr($fileName, "."), 1);
if($ext != "jpg" || $ext != "jpeg" || $ext != "JPG"){ error(); }
?>
As of now, nothing is happening when another filetype (.png, .gif, .doc, etc…) is uploaded. The error() function stops the rest of the page from working, but it is not show the error alert to users.
Here is a simple snippet for “white-listing” file extensions –
Only the file extensions in the
$extWhitelistwill be allowed.Resources –
pathinfo()in_array()