【问题标题】:Prevent PHP script from being executed when submitting to self提交给self时防止PHP脚本被执行
【发布时间】:2011-09-27 16:30:27
【问题描述】:

我有这个表格:

<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>

这是验证图片的 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";
  }
  ?>

我是将表单提交到同一页面,因此网页加载后立即执行 PHP。提交表单后如何使其加载?此外,此脚本似乎不起作用。

【问题讨论】:

  • 不清楚你想做什么!?
  • 阻止脚本立即执行
  • oOoK!我建议你 jahufar 回答!在处理您的 php 之前,您需要验证表单是否已提交!使用 if( isset($_POST('submit')) ),把你的整个代码放在这个 if 语句中

标签: php forms file file-upload


【解决方案1】:

在处理文件上传之前,您需要检查您的表单是否已提交:

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

  //save file here.

}

编辑:看起来您没有引用正确的 POST 变量 - 您的表单中有一个名为“pic”的文件元素,但您在 PHP 代码中引用的 $_POST['file'] 将不存在。

另外:如果你是从 PHP 开始的,(恕我直言)W3Schools.com 是最糟糕的地方 - 我已经看到了不应该在那里编写代码的非常糟糕的例子..

【讨论】:

  • 我想这会起作用,但我的实际脚本与我上面发布的脚本有些不同。我使用 Javascript (submit()) 提交表单,而不是提交按钮。提交表单后,页面将重置并且不显示错误消息。
  • 无论您如何提交都无所谓。提交后它仍然会点击comment.php 我所说的仍然适用(您需要检查表单是否已提交,然后才能开始处理它)。如果您没有收到任何警告,您可能需要在开发环境的 php.ini 中检查 display_errors 是否设置为 1 和 error_reporting 是否设置为 E_ALL
【解决方案2】:
<?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";
  }
}
  ?>

【讨论】:

    【解决方案3】:

    将此添加到页面顶部:

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

    您的新表单:

    <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>
    

    还有动作脚本:

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

    【讨论】:

    • 我猜没关系 :D 只是阅读了上面的答案 :D,猜想这对 javascript 没有帮助,但如果能提前知道这一点会很高兴 :D。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-08
    • 2022-10-18
    • 2014-11-29
    • 2014-02-05
    • 2012-12-21
    • 2016-11-23
    相关资源
    最近更新 更多