Reputation: 3232
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"] . "<br>";
}
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
Upvotes: 1
Views: 2640
Reputation: 20091
For other people that end up here: It's possible that the pictures with capital letters are from a different source (a camera with different settings) than the ones with lower case letters.
The ones with capital letters may be too large file size for example and you are not properly checking the $_FILES['file']['error']
property.
Check if you have an error in $_FILES['file']['error']
and compare to the list of errors: http://php.net/manual/en/features.file-upload.errors.php
Upvotes: 0
Reputation: 360742
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...
Upvotes: 2
Reputation: 39
Where is $filesize defined? If you added JPEG in caps, should have worked.
Upvotes: 0
Reputation: 307
$extension = end(explode(".", $_FILES["file"]["name"]));
I think this is causing error. change it to, $extension = strtolower(end(explode(".", $_FILES["file"]["name"])));
//change all into lowercase and then test.
Upvotes: 0