Hello, i'm a newbie at php and i'm trying to write a php image upload script that basically uploads specific image file types and deny all other files types. The code i have so far seems workable but it doesn't work for some reason, this is the code i have at the moment:
<?php
$target = "upload/";
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$image=$_FILES['image']['name'];
$filename = stripslashes($_FILES['image']['name']);
$allowed = array("jpeg","gif","png","bmp","jpg");
$extension = getExtension($filename);
$extension = strtolower($extension);
//check for valid extension
if (!in_array($extension == $allowed [0],[1],[2],[3],[4],)
{
echo "<h1>Sorry this file type is not supported. Please read the installation guide for a list of accepted file types.
<br>You will now be redirected to the main page.</h1>";
}
else
{
echo "<h1>Upload Successful!<br>You will now be redirected to the main page.</h1>";
}
?>
When i try to upload the image i get a parse error in line 31 which is
" if (!in_array($extension == $allowed [0],[1],[2],[3],[4],)". Can someone tell me why it's not working and also some suggestions to make this script better will also be good 🙂