不要 运行 在页面加载时发出警报

Not To Run Alert On Page Load

我从 w3 粘贴了这个上传代码副本用于图像上传并包含在一个页面中。问题是,当页面第一次加载时,所有警告框都会被触发 运行。我认为问题可能是因为 $uploadok 不是 ==。但是我该如何解决这个问题,这样警报就不会 运行 开始了。

我实际上是一个初学者,所以对新逻辑有点困惑。

所以这是代码

<?php
session_start();
$path = "C:\wamp64\www\Allian\users/".$_SESSION['username']."/uploads";
if (!file_exists($path)) {
    mkdir($path, 0700);
}
$target_dir = "users/".$_SESSION['username']."\uploads/";
$target_file = $target_dir . basename($_FILES["dp_btn"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if($_SERVER["REQUEST_METHOD"]=="POST") {
  if(isset($_POST["btn_save_changes"])) {
    $check = getimagesize($_FILES["dp_btn"]["tmp_name"]);
    if($check !== false) {
      echo "<script>alert('File is an image - " . $check["mime"] . ".')</script>";
      $uploadOk = 1;
    } else {
        echo "<script>alert('File is not an image.')</script>";
        $uploadOk = 0;
      }
  }
}
// Check if file already exists
if (file_exists($target_file)) {
  echo "<script>alert('Sorry, file already exists.')</script>";
  $uploadOk = 0;
}
// Check file size
if ($_FILES["dp_btn"]["size"] > 500000) {
  echo "<script>alert('Sorry, your file is too large.')</script>";
  $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
  echo "<script>alert('Sorry, only JPG, JPEG, PNG & GIF files are allowed.')</script>";
  $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
  echo "<script>alert('Sorry, your file was not uploaded.')</script>";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["dp_btn"]["tmp_name"], $target_file)) {
    echo "<script>alert('The file ". basename( $_FILES["dp_btn"]["name"]). " has been uploaded.')</script>";
  } else {
      echo "<script>alert('Sorry, there was an error uploading your file.')</script>";
    }
  }
?>

谢谢

将您不想 运行 放在 if(isset($_POST["btn_save_changes"])) { 块内的第一页加载的所有内容。这将确保它在回发时仅 运行s 并且 "save changes" 按钮被按下。当您第一次在浏览器中加载页面时,它总是通过 GET 完成的。 POST 仅在您提交表单时使用。除非你复制的演示有问题,否则我很惊讶它没有那样做。

<?php
session_start();
$path = "C:\wamp64\www\Allian\users/".$_SESSION['username']."/uploads";
if (!file_exists($path)) {
    mkdir($path, 0700);
}
$target_dir = "users/".$_SESSION['username']."\uploads/";
$target_file = $target_dir . basename($_FILES["dp_btn"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

// Check if image file is a actual image or fake image
if($_SERVER["REQUEST_METHOD"]=="POST") {
  if(isset($_POST["btn_save_changes"])) {
    $check = getimagesize($_FILES["dp_btn"]["tmp_name"]);
    if($check !== false) {
      echo "<script>alert('File is an image - " . $check["mime"] . ".')</script>";
      $uploadOk = 1;
    } else {
        echo "<script>alert('File is not an image.')</script>";
        $uploadOk = 0;
    }
    // Check if file already exists
    if (file_exists($target_file)) {
      echo "<script>alert('Sorry, file already exists.')</script>";
      $uploadOk = 0;
    }
    // Check file size
    if ($_FILES["dp_btn"]["size"] > 500000) {
      echo "<script>alert('Sorry, your file is too large.')</script>";
      $uploadOk = 0;
    }
    // Allow certain file formats
    if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
      echo "<script>alert('Sorry, only JPG, JPEG, PNG & GIF files are allowed.')</script>";
      $uploadOk = 0;
    }
    // Check if $uploadOk is set to 0 by an error
    if ($uploadOk == 0) {
      echo "<script>alert('Sorry, your file was not uploaded.')</script>";
      // if everything is ok, try to upload file
    } else {
      if (move_uploaded_file($_FILES["dp_btn"]["tmp_name"], $target_file)) {
       echo "<script>alert('The file ". basename( $_FILES["dp_btn"]["name"]). " has been uploaded.')</script>";
        } else {
            echo "<script>alert('Sorry, there was an error uploading your file.')</script>";
        }
    }
  }
}
?>

P.S。顺便说一句:作为用户体验,按顺序接收一系列包含错误消息的不同弹出窗口并不是特别有用。它们需要一个一个地消除,这很乏味,而且用户可能不记得之前的那些中有什么能够对其进行操作。您会发现很少有现代网站以这种方式显示错误(如果有的话)。更好的方法是建立一个包含所有错误消息的 HTML 列表,然后将其回显到页面中合适的位置,在那里它是可见的,并且用户可以在尝试更正时立即看到所有内容并重新提交表格..