How to check if the file uploaded file is solely an image and not video file and anything?
I tested out a simple code that will check if the uploaded file is an image.
form
<form method="post" enctype="multipart/form-data">
<input type="file" name="photo" accept="image/*">
<input type="submit" name="submit" value="Test">
</form>
Although I have accept="image/*"
, I can easily change it to All types
and get a non-image file.
code to check if the file is a valid image (This is only for testing)
if($_POST['submit']) {
$tmp_file = $_FILES['photo']['tmp_name'];
if(mime_content_type($tmp_file)) {
var_dump(mime_content_type($tmp_file));
} else {
echo 'error1';
}
if(getimagesize($tmp_file)) {
var_dump(getimagesize($tmp_file));
} else {
echo 'error2';
}
}
As of now I have 3 test to this, 2 passed 1 failed:
- Testing an image = passed.
- Testing a non-image valid .srt file = passed (gives an error to
getimagesize
) - Testing a valid video file .mp4 = failed (after the submission, it is loading for 5-10 seconds and not gives any error)
What do I need to do about this? I do not know what is the problem because it does not gives any results from both var_dump()
and echo 'errors'
. What I do think now is that PHP is accepting the file.
Note
I need to do this so that only a valid image will be uploaded.
Note 2
The accepted answer on the marked question is not working with me.
Update
If I try to upload a video that is less than 128M it returns something. But if it is greater than 128M it gets nothing in my localhost
, but I tested this in a production site it gives me a REQUEST TIME OUT
.