【问题标题】:Wordpress adding file import - Cannot modify header information - headers already sentWordpress 添加文件导入 - 无法修改标题信息 - 标题已发送
【发布时间】:2018-08-22 15:35:33
【问题描述】:

我正在自定义一个 wordpress 页面以添加导入 csv 文件功能。而且我遇到了“无法修改标头信息 - 标头已发送”错误。不知道我在哪里做错了,也不知道如何检查。

这是我尝试自定义的 php 模板文件。 wordpress 需要一些部分。但它似乎不是导致问题的原因,因为在我删除这些部分后它仍然收到错误消息。

<?php /* Template Name: csvImportTmp*/ ?>

<?php 
session_start();
//get_header(); 
?>


<?php
/*This part is for importing csv button*/
//error checking
ini_set('display_errors', 1);
error_reporting(E_ALL);



$connect = mysqli_connect("localhost","username","password","db_name"); //initiate connection 
if(isset($_POST["submit"])) //if submit button is pressed 
{
  if($_FILES['file']['name']) //if file exists
  {
    $filename=explode('.', $_FILES['file']['name']);//seperate file into filename and csv
    if($filename[1]=='csv'){  //if file format is csv
      $handle= fopen($_FILES['file']['tmp_name'], "r");
      while($data=fgetcsv($handle)){
        $sql="INSERT INTO val_in (xxx,xxx,xxx,xxx,xxx,xxx) VALUES(?,?,?,?,?,?)";
        //prepared statement 
        $stmt=mysqli_stmt_init($connect);
        if(!mysqli_stmt_prepare($stmt,$sql)){
          // echo "SQL prepared statement error";
        }
        else{
          mysqli_stmt_bind_param($stmt,"ssssss",$data[0],$data[1],$data[2],$data[3],$data[4],$data[5]);
          mysqli_stmt_execute($stmt);
        }
        mysqli_query($connect,$sql);
      }
      fclose($handle);
        //print "import done";
    }
  }
  header("Location: http://localhost:8888/xxx/wordpress/xxx/?file=test.csv&submit=Import");
  return;
}

?>

  <div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">

      <?php
      while ( have_posts() ) : the_post();

        get_template_part( 'template-parts/content', 'page' );

        // If comments are open or we have at least one comment, load up the comment template.
        if ( comments_open() || get_comments_number() ) :
          comments_template();
        endif;

      endwhile; // End of the loop.
      ?>

    </main><!-- #main -->
  </div><!-- #primary -->

  <p id="genValRes"><span id="genValRes"></p>
  <p id="clearValIn"><span id="clearValIn"></p>

  <div>
    <button id='genRes' onclick="genValRes();" name="genRes" class="button">Generate Result</button> 
    <button id='genRes' onclick="clearValIn();" name="genRes" class="button">Clear Input Table</button>  

  </div>


  <div>
    <form method='POST' enctype='multipart/form-data'>

       <div align="center">  
          <label>Import CSV File:</label>
          <input type="file" name="file" />
          <br />
          <input type="submit" name="submit" value="Import" class="btn btn-info" />
         </div>


    </form>  
  </div>

<?php
news_portal_get_sidebar();
get_footer();

【问题讨论】:

  • header("Location: http://localhost:8888/xxx/wordpress/xxx/?file=test.csv&amp;submit=Import");: 这行应该抛出错误。当您的模板正在加载时,标头已经发送,尝试挂钩一些操作,该操作较早从 functions.php 文件或其他地方触发

标签: php html wordpress session


【解决方案1】:

ob_start(); 放入 wp-config.php

【讨论】:

    猜你喜欢
    • 2011-11-14
    • 1970-01-01
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多