简体   繁体   English

提交给自己时防止执行PHP脚本

[英]Prevent PHP script from being executed when submitting to self

I have this form:我有这个表格:

<form name="commentform" id="commentform" action="comment.php" method="post" 
enctype="multipart/form-data">

Your Name: 
<textarea maxlength="60" rows="1" cols="62" class="margin" name="name" 
id="name"> </textarea> <br><br>

Submit Picture
<input type="file" name="pic" id="pic" /> <br><br>

<input type="Submit" value="Submit" />
</form>

This is the PHP to validate the picture (from W3Schools.com):这是用于验证图片的 PHP(来自 W3Schools.com):

<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

if (file_exists("upload/" . $_FILES["file"]["name"]))
  {
  echo $_FILES["file"]["name"] . " already exists. ";
  }
else
  {
  move_uploaded_file($_FILES["file"]["tmp_name"],
  "upload/" . $_FILES["file"]["name"]);
  echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
  }
  }
  }
  else
  {
  echo "Invalid file";
  }
  ?>

I am the submitting the form to the same page, so the PHP is executed as soon as the webpage loads.我将表单提交到同一页面,因此网页加载后立即执行 PHP。 How can I make it load as soon as the form is submitted?提交表单后如何使其加载? Also, this script does not seem to be working.此外,此脚本似乎不起作用。

You need to check if your form is submitted before you process the file upload:在处理文件上传之前,您需要检查您的表单是否已提交:

if ( isset($_POST['pic'])) {

  //save file here.

}

EDIT: It looks like your not referring to the right POST variable - you have a file element called 'pic' in your form but you are referring to $_POST['file'] in your PHP code which will not exist.编辑:看起来您没有引用正确的 POST 变量 - 您的表单中有一个名为 'pic' 的文件元素,但您在 PHP 代码中引用了$_POST['file'] ,该代码将不存在。

Also: If you are starting out with PHP, (IMHO) W3Schools.com is the worse place you can be - I've seen really bad examples of how code should NOT be written in there..另外:如果您从 PHP 开始,(恕我直言)W3Schools.com 是您可能遇到的更糟糕的地方 - 我已经看到了不应该在那里编写代码的非常糟糕的例子。

<?php

if( isset( $_POST( 'submit' ) ) ){ // Check form is submitted or not 

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

if (file_exists("upload/" . $_FILES["file"]["name"]))
  {
  echo $_FILES["file"]["name"] . " already exists. ";
  }
else
  {
  move_uploaded_file($_FILES["file"]["tmp_name"],
  "upload/" . $_FILES["file"]["name"]);
  echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
  }
  }
  }
  else
  {
  echo "Invalid file";
  }
}
  ?>

Add This To the Top of your page:将此添加到页面顶部:

<?php $action = $_GET['action']; ?>

Your New Form:您的新表格:

<form name="commentform" id="commentform" action="comment.php?action=go" method="post" enctype="multipart/form-data">
Your Name: <textarea maxlength="60" rows="1" cols="62" class="margin" name="name" id="name"> </textarea> <br><br>

Submit Picture<input type="file" name="pic" id="pic" /> <br><br>
<input type="Submit" value="Submit" />
</form>

And the action script:和动作脚本:

<?php
if (isset($action) && $action == 'go'){
if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 20000)) {
if ($_FILES["file"]["error"] > 0){
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}else{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))  {
echo $_FILES["file"]["name"] . " already exists. ";  
}else{  
move_uploaded_file($_FILES["file"]["tmp_name"],  "upload/" . $_FILES["file"]["name"]);  
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];  
}  
}  
}else{  
echo "Invalid file";  
}  
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM