Php 正在绕过空签入条件并正在运行
我有一个表单,用户可以附加一个文件并发送。我注意到,当用户提交没有附件的表单时,此条件仍在运行:Php 正在绕过空签入条件并正在运行,php,if-statement,conditional-statements,Php,If Statement,Conditional Statements,我有一个表单,用户可以附加一个文件并发送。我注意到,当用户提交没有附件的表单时,此条件仍在运行: if (!empty($_FILES['uploadedFile']['name']) && $_FILES['uploadedFile']['error'] != 4) 由于文件名为空,我无法理解为什么要运行它 有人知道为什么这种情况仍然存在吗 if ($_FILES['uploadedFile']['error'] == 1) { $error = "Th
if (!empty($_FILES['uploadedFile']['name']) && $_FILES['uploadedFile']['error'] != 4)
由于文件名为空,我无法理解为什么要运行它
有人知道为什么这种情况仍然存在吗
if ($_FILES['uploadedFile']['error'] == 1) {
$error = "The file {$_POST['first_name']} attempted to upload is too large. Contact them for an alternate way to send this file.";
$template = str_replace("{filename}", "$error", $template);
}
if (!empty($_FILES['uploadedFile']['name']) && $_FILES['uploadedFile']['error'] != 4) {
$fu = new fileUpload();
$filename = $fu->upload();
$out = (count($filename) > 1 ? 'Multiple files were' : 'A file was'). ' uploaded. You can download ' . (count($filename) > 1 ? 'them' : 'the file'). ' from:</ul>';
foreach ($filename as $indFile) {
$out .= "<li><a href='/uploads/{$indFile}'>{$indFile}</a></li>";
}
$out .= '</ul>';
$template = str_replace("{filename}", $out, $template);
clearstatcache();
} else {
$template = str_replace("{filename}", "", $template);
}
新代码:
foreach ($_FILES as $file) {
foreach($file['name'] as $key => $value) {
if ($value == "") { //empty string
if($file['error'][$key] != 4) { //error code is not #4
//do your code here, I did not check your other code
echo "name is empty AND error is NOT 4";
} else { //error code IS #4
echo "error code is 4";
}
} else {
echo "name is NOT empty!";
$fu = new fileUpload();
$filename = $fu->upload();
$out = (count($filename) > 1 ? 'Multiple files were' : 'A file was'). ' uploaded. You can download ' . (count($filename) > 1 ? 'them' : 'the file'). ' from:</ul>';
foreach ($filename as $indFile) {
$out .= "<li><a href='uploads/{$indFile}'>{$indFile}</a></li>";
}
$out .= '</ul>';
$template = str_replace("{filename}", $out, $template);
}
}
}
clearstatcache();
我认为问题在于你如何检查文件 下面的内容应该可以工作,而不必重做代码或实现高级错误处理 使用我根据转储外观构建的自定义$_FILES数组,将多个文件分解为多个数组,并将多个文件嵌套在“uploadedFile”方法下,它们都可以工作 方法1 方法2
您是否完成了var_dump$文件;如果提交时没有文件来检查它实际包含的内容,可以将其放在if语句之前。其实这并不重要,因为你说if语句的计算结果总是true。问题是你检查它的时候好像只得到一个文件,而你的表单是为多个文件设置的。检查['uploadedFile']['name']是否为空,而不是空的。它包含一个非空数组。好了,它包含一个非空数组。所以它本身不是空的…从输入名称中删除[]无论我把代码放在你说的地方,还是最后一个else语句中我认为应该放在的地方,因为名称不会是空的,文件应该上传,它仍然一直运行。你是否在表单中自然提交表单?或者使用JavaScript?Ajax提交。试试我上面编辑的版本,它会输出什么吗?其中一个骰子应该捕获它。带有此回显错误代码的else语句为4,正在输出。但是,它下面的else正在运行,因为我收到的电子邮件中包含了我的$out消息。我刚刚用我的代码更新了我的问题。
array(1) {
["uploadedFile"]=> array(5) {
["name"]=> array(1) {
[0]=> string(0) ""
}
["type"]=> array(1) {
[0]=> string(0) ""
}
["tmp_name"]=> array(1) {
[0]=> string(0) ""
}
["error"]=> array(1) {
[0]=> int(4)
}
["size"]=> array(1) {
[0]=> int(0)
}
}
}
foreach ($_FILES as $file) {
foreach($file['name'] as $key => $value) {
if ($value == "") { //empty string
if($file['error'][$key] != 4) { //error code is not #4
//do your code here, I did not check your other code
echo "name is empty AND error is NOT 4";
} else { //error code IS #4
echo "error code is 4";
}
} else {
echo "name is NOT empty!";
$fu = new fileUpload();
$filename = $fu->upload();
$out = (count($filename) > 1 ? 'Multiple files were' : 'A file was'). ' uploaded. You can download ' . (count($filename) > 1 ? 'them' : 'the file'). ' from:</ul>';
foreach ($filename as $indFile) {
$out .= "<li><a href='uploads/{$indFile}'>{$indFile}</a></li>";
}
$out .= '</ul>';
$template = str_replace("{filename}", $out, $template);
}
}
}
clearstatcache();
$_FILES = array(
'uploadedFile' => array(
'name' => array(0 => ""),
'error' => array(0 => 2)
),
'uploadedFile2' => array(
'name' => array(0 => "aasd"),
'error' => array(0 => 4)
),
'uploadedFile3' => array(
'name' => array(0 => ""),
'error' => array(0 => 4)
)
);
$_FILES = array(
'uploadedFile' => array(
'name' => array(
0 => "",
1 => "aasd",
2 => ""
),
'error' => array(
0 => 2,
1 => 4,
2 => 4
)
//...
)
);
<?php
foreach ($_FILES as $file) {
foreach($file['name'] as $key => $value) {
if ($value != null) { //non-empty string
if($file['error'][$key] != 4) { //error code is not #4
die ("name is NOT empty AND error is NOT 4");
} else { //error code IS #4
die("error code is 4");
}
} else {
die("name is EMPTY!");
}
}
}
?>