Php 不在页面加载时运行警报
我有这个上传代码副本粘贴从w3的图像上传,并包括在一个页面。问题是,当第一次加载页面时,所有警报框都会被触发并运行。我认为问题可能是因为$uploadok不是==。但如何解决此问题,使警报不会在启动时运行 我实际上是一个初学者,所以它有点混乱来与新的逻辑 这是代码Php 不在页面加载时运行警报,php,alert,Php,Alert,我有这个上传代码副本粘贴从w3的图像上传,并包括在一个页面。问题是,当第一次加载页面时,所有警报框都会被触发并运行。我认为问题可能是因为$uploadok不是==。但如何解决此问题,使警报不会在启动时运行 我实际上是一个初学者,所以它有点混乱来与新的逻辑 这是代码 <?php session_start(); $path = "C:\wamp64\www\Allian\users/".$_SESSION['username']."/uploads"; if (!file_exists($p
<?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($\u POST[“btn\u save\u changes”]){
块中。这将确保它只在回发时运行,并且“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>";
}
}
}
}
?>
您使用的是什么Web服务器?@Jasonbamber为什么这会有关系?不管怎样,PHP(和JavaScript)都将以相同的方式执行。无论如何,目录名中的“wampserver”会泄露它。@ADyson因为我怀疑他根本没有使用它,或者它设置错误(因此PHP没有被解释,所有的js都会同时启动)@Jasonbamber好吧,这不受它是哪个Web服务器的影响。它会受到PHP是否安装和运行的影响。这是一个更好的问题。但我认为描述中说的“全部”这些警报可能有误导性。我怀疑其中有几个警报正在显示,但不是全部。更可能是几个括号放错了位置-请参阅我的答案。此外,如果你的建议是这样的话,我怀疑OP也会报告他们的PHP代码在屏幕上被浪费是一个问题。以及“wampserver”表示肯定有服务器参与HNX。不知道如何回答。如果答案对您有帮助,请单击问题旁边的绿色勾号,使其变为绿色。这将标记答案为“已接受”(表示您认为它对您有效(如果有多个答案,则是最有用的答案)。请参阅