Error on file type
Here is my code
if ($_FILES['foto']['size'] > 200000) die ('file too big');
if($_FILES['foto']['type'] !== "image/pjpeg" ||
$_FILES['foto']['type'] !=="image/jpeg" || $_FILES['foto']['type']
!=="image/gif") die ('file not allowed!');
no matter what i upload it always showing "file not allowed". It works fine if i delete the file checking part.
I wonder where are the mistakes.
Thanks
== update=== var dump string(10) "image/jpeg开发者_Go百科"
This is a contradiction (Always False):
$_FILES['foto']['type'] !== "image/pjpeg" ||
$_FILES['foto']['type'] !== "image/jpeg" ||
$_FILES['foto']['type'] !== "image/gif"
Since for the whole statement to be true $_FILES['foto']['type']
Must be equal to "image/pjpeg", "image/jpeg", and "image/gif" at the same time.
You mean to be using &&
instead of ||
, so you get:
$_FILES['foto']['type'] !== "image/pjpeg" &&
$_FILES['foto']['type'] !== "image/jpeg" &&
$_FILES['foto']['type'] !== "image/gif"
You should log $_FILES['foto']['size'] and $_FILES['foto']['type']. That will at least let you know what is being used during your comparisons.
Can't figure out how to do a multiline edit of my comment:
if ($_FILES['foto']['size'] > 200000) die ('file too big');
if($_FILES['foto']['type'] !== "image/pjpeg" ||
$_FILES['foto']['type'] !=="image/jpeg" || $_FILES['foto']['type']
!=="image/gif") die ('file not allowed!');
Maybe try this:
if ($_FILES['foto']['size'] > 200000) die ('file too big');
$file_types = array("image/pjpeg", "image/jpeg", "image/"gif");
if(!in_array($_FILES['foto']['type'],$file_types)) die ('not allowed');
精彩评论