0

次の数行があります。

if ( (empty($_FILES["userFile1"]) ) or ( empty($_FILES["userFile2"]) ) or ( empty($_FILES["userFile2"]) ) ) {
    header("Location: " . "/");
}

// required fields
$required = array("userName", "userAddress", "userEmail");

// Loop over field names, make sure each one exists and is not empty
$error = false;
foreach ($required as $field) {
  if (empty($_POST["$field"])) {
    $error = true;
  }
}

// if error occurs
if ($error === true) {
    header("Location: " . "/");
}

ただし、ユーザーが 3 つのファイルすべてをアップロードしなくても、またはユーザーがフィールドを空白のままにしても、スクリプトは続行されます (スクリプトの後半の副作用でわかります)。これらが行う唯一のことはユーザーをリダイレクトすることであるため、明らかにどちらのチェックもパスしません。

しかし、フィールドが空の場合、またはファイルがアップロードされていない場合、チェックが機能しないのはなぜでしょうか?

4

2 に答える 2

1

もしかして出る?

header("Location: " . "/");
exit;

HTTP リダイレクトがブラウザに送信されますが、PHP スクリプトは引き続き実行されます。リダイレクト後は常に exit が必要です。

于 2013-11-02T00:13:17.610 に答える
1

これを試して

if ( (!isset($_FILES["userFile1"]) ) or ( !isset($_FILES["userFile2"]) ) or ( !isset($_FILES["userFile2"]) ) ) {
    header("Location: " . "/");
    exit;
}

// required fields
$required = array("userName", "userAddress", "userEmail");

// Loop over field names, make sure each one exists and is not empty
$error = false;
foreach ($required as $field) {
  if (!isset($_POST["$field"])) {
    $error = true;
    break;
  }
}

// if error occurs
if ($error === true) {
    header("Location: " . "/");
    exit;
}
于 2013-11-02T00:13:26.257 に答える