我正在自定义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();
答案 0 :(得分:1)
ob_start(); 放入wp-config.php