Php 在从同一个$#u文件数组获取两个不同的[';类型';]验证时,是否可以设置它们?
在我的表格中,我要求某人上传一个PDF文件和一张图片。它们都被发送到同一个数组Php 在从同一个$#u文件数组获取两个不同的[';类型';]验证时,是否可以设置它们?,php,Php,在我的表格中,我要求某人上传一个PDF文件和一张图片。它们都被发送到同一个数组$\u文件['rfiles']数组。我想将$\u文件['rfiles']['type'][0]具体验证为“application/pdf”或“text/plain”,并将$\u文件['rfiles']['type'][1]验证为“image/*”。这第一部分是代码的工作部分。我正试图最大限度地使用(Foreach/[$key])。我试图在不使用表单NAME='rfiles[].的情况下发送这两个文件,但是$\u fil
$\u文件['rfiles']
数组。我想将$\u文件['rfiles']['type'][0]
具体验证为“application/pdf”或“text/plain”,并将$\u文件['rfiles']['type'][1]
验证为“image/*”。这第一部分是代码的工作部分。我正试图最大限度地使用(Foreach/[$key
])。我试图在不使用表单NAME='rfiles[].
的情况下发送这两个文件,但是$\u files
有一个选项。因此,似乎无论如何都必须使用Foreach方法
foreach ($_FILES['rfiles']['error'] as $key => $error) {
$tmp_name = $_FILES['rfiles']['tmp_name'][$key];
if (!$tmp_name) continue;
$name = basename($_FILES['rfiles']['name'][$key]); #the recycled variable
#This is where the other code would rest.
if ($error == UPLOAD_ERR_OK)
{
if (file_exists("/restdata/" . $_FILES["rfiles"]["name"][$key])) #Checking for a duplicate filename
{
echo $_FILES["rfiles"]["name"][$key] . " is currenty in use. Please rename your file to a unique name and try again.<br />\n";$badul[$key]=1; #badul[] is to be used later in the page in the followup form.
}
else
{
if ( move_uploaded_file($tmp_name, "/restdata/".$name) ) {
$uploaded_array[] .= "Uploaded file '".$name."'.<br/>\n";
echo "File is valid, and was successfully uploaded.\n<br />\n";
echo ($_FILES["rfiles"]["type"][$key]. " is a proper format.<br />\n"); #Mostly being used for testing purposes to get the correct MIME. But, how do I validate it?
echo ($uploaded_array[$key] . "<br>\n");
}
else
{
$errormsg .= "Could not move uploaded file '".$tmp_name."' to '".$name."'<br/>\n";
} # end if/else move_upoaded File
} #end if/else file_exists
} #end if $error == UPLOAD_ERR_OK
else $errormsg .= "Upload error. [".$error."] on file '".$name."'<br/>\n";
echo ( $errormsg );
} #End Foreach
另一种方法留下了太多的漏洞,因为如果其中一个参数通过,那么第二个文件是什么并不重要:
...
if (((($_FILES["rfiles"]["type"][1] == "image/gif")
|| ($_FILES["rfiles"]["type"][1] == "image/jpeg")
|| ($_FILES["rfiles"]["type"][1] == "image/pjpeg"))
&& ($_FILES["rfiles"]["size"][1] < 5000000))
|| ((($_FILES["rfiles"]["type"][0] == "text/plain") #probaby the wrong syntax, but worth a shot.
|| ($_FILES["rfiles"]["type"][0] == "application/pdf")
|| ($_FILES["rfiles"]["type"][0] == "text/rtf"))
&& ($_FILES["rfiles"]["size"][0] < 15000000)))
{
if ($error == UPLOAD_ERR_OK)...
。。。
如果(($_文件[“rfiles”][“type”][1]=“image/gif”)
||($_文件[“rfiles”][“type”][1]=“image/jpeg”)
||($_文件[“rfiles”][“type”][1]=“image/pjpeg”))
&&($_文件[“rfiles”][“size”][1]<5000000))
||(($_FILES[“rfiles”][“type”][0]==“text/plain”)#可能语法错误,但值得一试。
||($_文件[“rfiles”][“type”][0]==“application/pdf”)
||($_文件[“rfiles”][“type”][0]=“text/rtf”))
&&($_文件[“rfiles”][“size”][0]<15000000)))
{
如果($error==UPLOAD\u ERR\u OK)。。。
我试图通过$\u文件直接攻击它
&试图将其分解..还有其他方法吗
编辑:出于澄清目的-我需要能够单独处理文件验证。它们的值正在单独处理到数据库中。无法向客户提供特定错误将给他们带来太多的混乱。用以下方法替换第二种方法,然后应该如预期的那样:
...
if (((($_FILES["rfiles"]["type"][1] == "image/gif")
|| ($_FILES["rfiles"]["type"][1] == "image/jpeg")
|| ($_FILES["rfiles"]["type"][1] == "image/pjpeg"))
&& ($_FILES["rfiles"]["size"][1] < 5000000))
&& ((($_FILES["rfiles"]["type"][0] == "text/plain") #probaby the wrong syntax, but worth a shot.
|| ($_FILES["rfiles"]["type"][0] == "application/pdf")
|| ($_FILES["rfiles"]["type"][0] == "text/rtf"))
&& ($_FILES["rfiles"]["size"][0] < 15000000)))
{
if ($error == UPLOAD_ERR_OK)...
。。。
如果(($_文件[“rfiles”][“type”][1]=“image/gif”)
||($_文件[“rfiles”][“type”][1]=“image/jpeg”)
||($_文件[“rfiles”][“type”][1]=“image/pjpeg”))
&&($_文件[“rfiles”][“size”][1]<5000000))
&&(($_FILES[“rfiles”][“type”][0]==“text/plain”)#可能语法错误,但值得一试。
||($_文件[“rfiles”][“type”][0]==“application/pdf”)
||($_文件[“rfiles”][“type”][0]=“text/rtf”))
&&($_文件[“rfiles”][“size”][0]<15000000)))
{
如果($error==UPLOAD\u ERR\u OK)。。。
这解决了一般的验证问题。遗憾的是,这留下了一个全有或全无的情况,那么我将无法单独处理文件进行验证。因此,初步想法是离开“| |”语句。再次回到“Foreach”的想法。
...
if (((($_FILES["rfiles"]["type"][1] == "image/gif")
|| ($_FILES["rfiles"]["type"][1] == "image/jpeg")
|| ($_FILES["rfiles"]["type"][1] == "image/pjpeg"))
&& ($_FILES["rfiles"]["size"][1] < 5000000))
&& ((($_FILES["rfiles"]["type"][0] == "text/plain") #probaby the wrong syntax, but worth a shot.
|| ($_FILES["rfiles"]["type"][0] == "application/pdf")
|| ($_FILES["rfiles"]["type"][0] == "text/rtf"))
&& ($_FILES["rfiles"]["size"][0] < 15000000)))
{
if ($error == UPLOAD_ERR_OK)...