I'm making an image uploader but for some reason i'm not allowed to upload JPG images in capital letters. How is this possible?
I also tried to add JPG to the allowedExts array but that is also not working.
$filesize = '8500'; // PUT the filesize here in KB
if(isset($_FILES["file"])){
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
var_dump($_FILES['file']['type']);
var_dump($extension);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < $filesize)
&& in_array($extension, $allowedExts)){
if ($_FILES["file"]["error"] > 0){
echo "Return Code: " . $_FILES["file"]["error"] . "
";
}
else{
if (file_exists("source/images/" . $_FILES["file"]["name"])){
echo 'image already exists';
}
else{
//Upload original file to folder
}
}
}
else{
echo 'Wrong fileformat';
}
As output I get this:
string '' (length=0)
string 'JPG' (length=3)
Wrong fileformat
解决方案
PHP string comparisons are case sensitive:
&& in_array($extension, $allowedExts)){
is going to blow up if you upload kitten.JPG, because .JPG is NOT in your allowed extensions array. .jpg is, but that's a completely different string as far as PHP is concerned. You should normalize the extension you get from the uploaded filename with strtolower, or at least use a case-insentive comparison, such as strcasecmp
And note that your file handling logic is incorrect. You've obviously grabbed a very widely distributed BAD example. The VERY first thing you need to check upon upload is the ['error'] parameter. If that's nonzero, then you cannot trust anything else in the $_FILES array for that particular file. Don't check size, don't check mime types, don't check filenames. If an upload fails, those could all be non-existent/incorrect/etc...