【问题标题】:Include file error in php在php中包含文件错误
【发布时间】:2013-06-28 03:08:20
【问题描述】:

我有一个简单的 php 问题。我想包含一个简单的 PHP 文件。我正在使用include("filename.php") 来包含该文件。

但问题是,如果文件包含正常,那么它会显示错误,但如果包含的文件中有任何错误,那么它就会死掉并且没有进一步的执行。

我希望如果包含的文件中有任何错误,它只会显示错误并且应该继续进一步执行:

就像在包含文件之后,我显示footer。目前,如果代码中有任何错误,则页脚不显示,如果我想在包含文件有错误时显示页脚。我该怎么做?

编辑

<?php

include("header.php");
include("dbconnect.php");

<div class="content">
    <div class="container">
    <div >
    <form name="savecode" action="" method="post">
<div class="span12">

<form action="#" method="post">
<input type="hidden" name="language" value="PHP">
<textarea id="code" name="yourcode" style="width:800px;height:200px;"><?php echo $code;?></textarea>
<br/><input type="submit" class="btn btn-primary" name="reruncode" value ="Run the code">
</div>
<hr/>
<div class="span12">
<h5>Output of the above code</h5>

<textarea id="output" style="width:800px;height:200px;" disabled="disabled">
<?php

include("testfile.php");
?>
</textarea>
<?php
if(isset($_SESSION['email']))
{
$uniquid = md5(uniqid($email,true));
echo "<input type='hidden' id='uniqid' value=$uniquid>";
?>
<input type="hidden" id="email" value="<?php echo $_SESSION['email']; ?>">
<br/><input id="submit" value="Save this code" class="btn btn-primary" />
<?php
}
?>
</div>
</div>
</div>

</div>
</div>

include("footer.php");

【问题讨论】:

标签: php


【解决方案1】:

如果包含文件中存在解析错误,则这是不可能的,执行将终止。尝试捕获异常也不起作用。进一步讨论here
如果它是包含文件中的一般错误,请确保尝试捕获包含文件中的所有可能异常,以便它正常失败,然后在这种情况下可以包含它。

【讨论】:

  • 有一种方法可以“捕获”包含文件中的致命错误stackoverflow.com/a/17160996/1503018
  • 如果你想清理东西,如果关闭,这似乎更像是一个自定义处理程序。之后就没有办法继续执行了吧?
  • 是的,是的。但是当你尝试做 ^that 员工时,你可以考虑这种方式。
【解决方案2】:

您需要将您的 HTML 放在一个字符串中或在 HTML 开始之前关闭 PHP 标记,并在 HTML 结束后再次启动 PHP 标记。

在您的代码中您错过了它。另一个是带有表单元素的表单。发布数据时它将不起作用。您的 Form 标签未在代码中的任何位置关闭。

例如

<?php

include("header.php");
include("dbconnect.php");

?>

<div class="content">
    <div class="container">
        <div>
            <form name="savecode" action="" method="post">
               <div class="span12">

                   <form action="#" method="post">
                       <input type="hidden" name="language" value="PHP">
                       <textarea id="code" name="yourcode" style="width:800px;height:200px;"><?php echo $code;?></textarea>
                       <br/><input type="submit" class="btn btn-primary" name="reruncode" value ="Run the code">
               </div>
               <hr/>
               <div class="span12">
                    <h5>Output of the above code</h5>

                     <textarea id="output" style="width:800px;height:200px;" disabled="disabled">
                     <?php include("testfile.php"); ?>
                     </textarea>
                     <?php
                     if(isset($_SESSION['email']))
                     {
                         $uniquid = md5(uniqid($email,true));
                         echo "<input type='hidden' id='uniqid' value=$uniquid>";
                     ?>
                         <input type="hidden" id="email" value="<?php echo $_SESSION['email']; ?>">
                         <br/><input id="submit" value="Save this code" class="btn btn-primary" />
                     <?php
                     }
                     ?>
                </div>
           </div>
       </div>

    </div>
</div>

<?php include("footer.php"); ?>

【讨论】:

  • 我已经解决了这个错误,但这并没有解决我的文件包含问题
  • 发布您遇到的错误。通过 init('display_errors', 1)error_reporting(E_ALL) 启用错误报告
  • 你是在收到我的问题还是在即时给出答案...我收到错误,但如果文件执行中出现错误,我想执行代码
  • 那么你需要使用 try catch 块,正如@sectus 在下面的 cmets 中指出的那样。
猜你喜欢
  • 1970-01-01
  • 2014-09-10
  • 1970-01-01
  • 1970-01-01
  • 2012-08-27
  • 1970-01-01
  • 1970-01-01
  • 2018-01-11
相关资源
最近更新 更多