在页面加载时不运行提醒

时间:2018-09-03 20:54:29

标签: php alert

我从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>";
    }
  }
?>

Thnx

1 个答案:

答案 0 :(得分:0)

将您不想在第一页加载上运行的所有内容放入if(isset($_POST["btn_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列表,然后将其回显到页面中合适的位置(可见),用户可以立即看到所有内容,同时尝试更正并重新提交表格。