[英]php file upload with validation type
In php I have my code for uploading files is like this 在PHP中,我有上传文件的代码是这样的
$image_name= $_FILES['file']['name'];
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& in_array($extension, $allowedExts)) {
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else {
move_uploaded_file($_FILES['file']['tmp_name'], $tmpName.$image_name);
}
}
Now this code is working fine when I am doing upload. 现在,当我执行上传时,此代码可以正常工作。 But it doesn't work with the filetype validation.
但这不适用于文件类型验证。 I have used
$allowedExts = array("gif", "jpeg", "jpg", "png"); to use only these types of file to upload. But this one is uploading any files type. So can someone kindly tell me where is the wrong part here. I want to upload only
我已经使用
$allowedExts = array("gif", "jpeg", "jpg", "png"); to use only these types of file to upload. But this one is uploading any files type. So can someone kindly tell me where is the wrong part here. I want to upload only
$allowedExts = array("gif", "jpeg", "jpg", "png"); to use only these types of file to upload. But this one is uploading any files type. So can someone kindly tell me where is the wrong part here. I want to upload only
$allowedExts = array("gif", "jpeg", "jpg", "png"); to use only these types of file to upload. But this one is uploading any files type. So can someone kindly tell me where is the wrong part here. I want to upload only
"gif", "jpeg", "jpg", "png" files only. $allowedExts = array("gif", "jpeg", "jpg", "png"); to use only these types of file to upload. But this one is uploading any files type. So can someone kindly tell me where is the wrong part here. I want to upload only
“ gif”,“ jpeg”,“ jpg”,“ png”文件。 Any help and suggestions will be really appreciale. 任何帮助和建议将是非常宝贵的。 Thanks.
谢谢。
I remember I had a problem with file types before and I couldn't resolve it so I decided to go on with something else. 我记得以前我遇到过文件类型问题,但无法解决,所以我决定继续进行其他工作。 This was one of my ways to check if the files being uploaded were "images" way before.
这是我检查上传的文件之前是否为“图像”的方法之一。 I don't think a great way to check if a file is actually an image, but it actually worked for me and I couldn't think of anything else by the time.
我认为检查文件是否实际上是图像的好方法并不多,但实际上它对我有用,到那时我什么也没想到。
<?php
if(isAnImage($routetoyourfile)) {
//Move the file
} else {
//Delete it
}
function isAnImage($fileroute) {
$ext = pathinfo($fileroute, PATHINFO_EXTENSION);
if($ext != "jpg" && $ext != "jpeg" && $ext != "png" && $ext != "JPG" && $ext != "PNG" && $ext != "JPEG") {
return 0;
} else {
return 1;
}
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.