0

I am customizing a wordpress page to add a import csv file function. And I have encountered the " Cannot modify header information - headers already sent" error. Not sure where I did wrong, and not sure how I can check this.

This the php template file I am trying to customize. There is a few parts that are required from the wordpress. But it does not seems like what's causing the problem since it still got the error message after I deleted those parts.

<?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();
1
  • header("Location: http://localhost:8888/xxx/wordpress/xxx/?file=test.csv&submit=Import");: this line should throw error. when your template is loading, the headers already sended, try to hook some action, which fires earlier from functions.php file, or somewhere else Commented Apr 18, 2018 at 7:34

1 Answer 1

1

ob_start(); put in wp-config.php

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.