dongluni0568 2018-09-03 20:54
浏览 83
已采纳

不在页面加载上运行警报

I have this upload code copy pasted from w3 for image upload and included in a page. The problem is, when the page gets loaded for the first time all the alert boxes get triggered and run. I think the problem is probably because of the $uploadok being not ==. But how do I solve this issue so the alert do not run on start.

I'm actually a beginner so its kinda confusing to come with new logics.

So here is the code

<?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条回答 默认 最新

  • dongwa3808 2018-09-03 21:10
    关注

    Put everything that you don't want to run on the first page load inside the if(isset($_POST["btn_save_changes"])) { block. That will ensure it only runs on postback and that the "save changes" button was pressed. When you first load the page in your browser it's always done via a GET. POST is only used when you submit a form. Unless the demo you copied from was faulty, I'm surprised it didn't do it like that already.

    <?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. Just an aside: As a user experience, receiving a series of different popups in sequence containing error messages is not a particularly helpful one. They require dismissing one by one which is tedious, and the user might not remember what was in the earlier ones to be able to act on it. You'll find very few modern websites display errors in this way (if any). A much better way would be to build up a HTML list containing all the error messages and then just echo that into a suitable location in the page, where it's visible, and the user can see everything at once whilst trying to correct and re-submit the form..

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?