标头下载实际页面内容,而不是我构建的 csv 文件

发布于 2024-12-29 06:21:34 字数 5183 浏览 1 评论 0原文

我的目标是让浏览器使用标头下载 csv 文件。由于某种尚未确定的原因,浏览器似乎正在下载当前页面的 HTML 内容(而不是我给它的数组的内容)。

这是我一直在使用的代码:

$arr1 = array(array("1","2","3","4"),array("2","1","6","6"));

$tmp_handle = fopen('php://memory', 'r+');
fputcsv($tmp_handle, $arr1);

header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");

rewind($tmp_handle);
echo stream_get_contents($tmp_handle);

我已经按照我读过的许多文章/SO问题的说明进行操作,但我不明白这段代码有什么问题。

我当然感谢我能在这里得到的任何帮助!

这是完整的代码(根据要求):

<?php
global $wpdb;

// Get total number of active referrers
$referrer_check = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."referrer");
$num_of_referrers = 0;

foreach ( $referrer_check as $check) 
{
$num_of_referrers++;
}

// Get total number of referral transactions
$num_of_referrals = 0;
$num_referral_check = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."referrer_transactions");
foreach ( $num_referral_check as $check) 
{
$num_of_referrals++;
}

// Check for the top referrer
$top_referrer = $wpdb->get_row("SELECT referrer_id, count(*) as row_count FROM ".$wpdb->prefix."referrer_transactions GROUP BY referrer_id ORDER BY COUNT(*) DESC");
$top_referrer_result = $wpdb->get_row("SELECT * FROM ".$wpdb->prefix."referrer WHERE referrer_id = $top_referrer->referrer_id");

// Construct the table

// Create array for second table
$ref_transactions_table_arr = array(
array("Referee Name", "Referee ID", "Referee Sign Up", "Referee Email","Referrer ID","Referrer Name"));

foreach ($num_referral_check as $check) 
{
$ref_transactions_table_arr[] = array(
$wpdb->get_var("SELECT billing_name FROM ".$wpdb->prefix."pmpro_membership_orders WHERE user_id = $check->buyer_id"),
$check->buyer_id,
$wpdb->get_var("SELECT user_registered FROM ".$wpdb->prefix."users WHERE ID = $check->buyer_id"),
$wpdb->get_var("SELECT user_email FROM ".$wpdb->prefix."users WHERE ID = $check->buyer_id"),
$wpdb->get_var("SELECT referrer_id FROM ".$wpdb->prefix."referrer WHERE referrer_id = $check->referrer_id"),
$wpdb->get_var("SELECT referrer_name FROM ".$wpdb->prefix."referrer WHERE referrer_id = $check->referrer_id")
);
}

// Create array for first table
$active_ref_table_arr = array(
array('Referrer Name', 'Referrer ID', '# of Referrals', 'Address','Referrer Email','Lifetime Referrals'));

foreach ( $referrer_check as $check) 
{
$active_ref_table_arr[] = array(
$check->referrer_name, 
$check->referrer_id,
$wpdb->get_var("SELECT count(*) FROM ".$wpdb->prefix."referrer_transactions WHERE referrer_id = $check->referrer_id"),
$check->referrer_street . " " . $check->referrer_city . ", " . $check->referrer_state . " " . $check->referrer_zip,
$wpdb->get_var("SELECT user_email FROM ".$wpdb->prefix."users WHERE ID= $check->referrer_id"),
$wpdb->get_var("SELECT count(*) FROM ".$wpdb->prefix."referrer_transactions WHERE referrer_id = $check->referrer_id")
);
}

// Download file
if(isset($_POST['export_tbl_one']))
{
$csvData = array(
  array("1","2","3","4"),
  array("2","1","6","6")
);

$fp = fopen('php://memory', 'w+');

/*foreach ($csvData as $row) {
  fputcsv($fp, $row);
}*/

fputcsv($fp,$csvData);

rewind($fp);
$csvFile = stream_get_contents($fp);
fclose($fp);

header('Content-Type: text/csv');
header('Content-Length: '.strlen($csvFile));
header('Content-Disposition: attachment; filename="file.csv"');

exit($csvFile);
}

?>
<div class="nav">
        <ul>
                <li class="first"><a href="#">Total Referrers: <? echo $num_of_referrers;  ?></a></li>
                <li><a href="#">Total Referals: <? echo $num_of_referrals; ?></a></li>
                <li><a href="#">Top Referrer: <? echo $top_referrer->referrer_id . ", " . $top_referrer_result->referrer_name . "(" . $top_referrer->row_count . ")"; ?></a></li>
<li>
<form method="POST" action="http://keepmecertified.com/acp">
<input type="submit" value="click me" name="export_tbl_one"/>
</form>

</li>
        </ul>
</div>

<br>

<table class="table">
<caption>Referrer Transactions</caption>
<?
$num = 0;

foreach($ref_transactions_table_arr as $fields)
{

  echo "<tr>";


    foreach($fields as $data)
    {

      if($num == 0)
      {
        echo "<th class=\"ref_head\">$data</th>";
      }
      else
      {
        echo "<td>$data</td>";
      }

    }


   echo "</tr>";

   if($num == 0)
   {
     $num++;
   }


}

?>
</table>

<table class="table">
<caption>Active Referrers</caption>
<?
$num = 0;

foreach($active_ref_table_arr as $fields)
{

  echo "<tr>";


    foreach($fields as $data)
    {

      if($num == 0)
      {
        echo "<th class=\"ref_head\">$data</th>";
      }
      else
      {
        echo "<td>$data</td>";
      }

    }


   echo "</tr>";

   if($num == 0)
   {
     $num++;
   }


}

?>
</table>

My goal here is to have the browser download a csv file using headers to do it. For some reason yet to be determined, the browser seems to be downloading the HTML content of the current page (and not the contents of the array I've given it).

Here is the code I've been using:

$arr1 = array(array("1","2","3","4"),array("2","1","6","6"));

$tmp_handle = fopen('php://memory', 'r+');
fputcsv($tmp_handle, $arr1);

header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");

rewind($tmp_handle);
echo stream_get_contents($tmp_handle);

I've followed the instructions of many articles / SO questions I've read and I don't see what's wrong with this code.

I of course appreciate any help that I can get here!

Here is the complete code (upon request):

<?php
global $wpdb;

// Get total number of active referrers
$referrer_check = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."referrer");
$num_of_referrers = 0;

foreach ( $referrer_check as $check) 
{
$num_of_referrers++;
}

// Get total number of referral transactions
$num_of_referrals = 0;
$num_referral_check = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."referrer_transactions");
foreach ( $num_referral_check as $check) 
{
$num_of_referrals++;
}

// Check for the top referrer
$top_referrer = $wpdb->get_row("SELECT referrer_id, count(*) as row_count FROM ".$wpdb->prefix."referrer_transactions GROUP BY referrer_id ORDER BY COUNT(*) DESC");
$top_referrer_result = $wpdb->get_row("SELECT * FROM ".$wpdb->prefix."referrer WHERE referrer_id = $top_referrer->referrer_id");

// Construct the table

// Create array for second table
$ref_transactions_table_arr = array(
array("Referee Name", "Referee ID", "Referee Sign Up", "Referee Email","Referrer ID","Referrer Name"));

foreach ($num_referral_check as $check) 
{
$ref_transactions_table_arr[] = array(
$wpdb->get_var("SELECT billing_name FROM ".$wpdb->prefix."pmpro_membership_orders WHERE user_id = $check->buyer_id"),
$check->buyer_id,
$wpdb->get_var("SELECT user_registered FROM ".$wpdb->prefix."users WHERE ID = $check->buyer_id"),
$wpdb->get_var("SELECT user_email FROM ".$wpdb->prefix."users WHERE ID = $check->buyer_id"),
$wpdb->get_var("SELECT referrer_id FROM ".$wpdb->prefix."referrer WHERE referrer_id = $check->referrer_id"),
$wpdb->get_var("SELECT referrer_name FROM ".$wpdb->prefix."referrer WHERE referrer_id = $check->referrer_id")
);
}

// Create array for first table
$active_ref_table_arr = array(
array('Referrer Name', 'Referrer ID', '# of Referrals', 'Address','Referrer Email','Lifetime Referrals'));

foreach ( $referrer_check as $check) 
{
$active_ref_table_arr[] = array(
$check->referrer_name, 
$check->referrer_id,
$wpdb->get_var("SELECT count(*) FROM ".$wpdb->prefix."referrer_transactions WHERE referrer_id = $check->referrer_id"),
$check->referrer_street . " " . $check->referrer_city . ", " . $check->referrer_state . " " . $check->referrer_zip,
$wpdb->get_var("SELECT user_email FROM ".$wpdb->prefix."users WHERE ID= $check->referrer_id"),
$wpdb->get_var("SELECT count(*) FROM ".$wpdb->prefix."referrer_transactions WHERE referrer_id = $check->referrer_id")
);
}

// Download file
if(isset($_POST['export_tbl_one']))
{
$csvData = array(
  array("1","2","3","4"),
  array("2","1","6","6")
);

$fp = fopen('php://memory', 'w+');

/*foreach ($csvData as $row) {
  fputcsv($fp, $row);
}*/

fputcsv($fp,$csvData);

rewind($fp);
$csvFile = stream_get_contents($fp);
fclose($fp);

header('Content-Type: text/csv');
header('Content-Length: '.strlen($csvFile));
header('Content-Disposition: attachment; filename="file.csv"');

exit($csvFile);
}

?>
<div class="nav">
        <ul>
                <li class="first"><a href="#">Total Referrers: <? echo $num_of_referrers;  ?></a></li>
                <li><a href="#">Total Referals: <? echo $num_of_referrals; ?></a></li>
                <li><a href="#">Top Referrer: <? echo $top_referrer->referrer_id . ", " . $top_referrer_result->referrer_name . "(" . $top_referrer->row_count . ")"; ?></a></li>
<li>
<form method="POST" action="http://keepmecertified.com/acp">
<input type="submit" value="click me" name="export_tbl_one"/>
</form>

</li>
        </ul>
</div>

<br>

<table class="table">
<caption>Referrer Transactions</caption>
<?
$num = 0;

foreach($ref_transactions_table_arr as $fields)
{

  echo "<tr>";


    foreach($fields as $data)
    {

      if($num == 0)
      {
        echo "<th class=\"ref_head\">$data</th>";
      }
      else
      {
        echo "<td>$data</td>";
      }

    }


   echo "</tr>";

   if($num == 0)
   {
     $num++;
   }


}

?>
</table>

<table class="table">
<caption>Active Referrers</caption>
<?
$num = 0;

foreach($active_ref_table_arr as $fields)
{

  echo "<tr>";


    foreach($fields as $data)
    {

      if($num == 0)
      {
        echo "<th class=\"ref_head\">$data</th>";
      }
      else
      {
        echo "<td>$data</td>";
      }

    }


   echo "</tr>";

   if($num == 0)
   {
     $num++;
   }


}

?>
</table>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灵芸 2025-01-05 06:21:34

尝试以下代码:

$csvData = array(
  array("1","2","3","4"),
  array("2","1","6","6")
);

$fp = fopen('php://memory', 'w+');
foreach ($csvData as $row) {
  fputcsv($fp, $row);
}

rewind($fp);
$csvFile = stream_get_contents($fp);
fclose($fp);

header('Content-Type: text/csv');
header('Content-Length: '.strlen($csvFile));
header('Content-Disposition: attachment; filename="file.csv"');

exit($csvFile);

我已循环数据来构建 CSV,因为您的代码不会产生您期望的结果。在输出之前,我还以字符串形式检索了文件 - 这只是添加 Content-Length 标头的一个好处。我还 - 这是重要的一点 - 称为 exit 来输出数据,以防止在输出此代码后执行任何其他代码。

如果您正在使用此代码但仍然遇到问题,则该代码不会被调用 - 您应该检查此代码所包含的任何 if 语句等。

Try this code:

$csvData = array(
  array("1","2","3","4"),
  array("2","1","6","6")
);

$fp = fopen('php://memory', 'w+');
foreach ($csvData as $row) {
  fputcsv($fp, $row);
}

rewind($fp);
$csvFile = stream_get_contents($fp);
fclose($fp);

header('Content-Type: text/csv');
header('Content-Length: '.strlen($csvFile));
header('Content-Disposition: attachment; filename="file.csv"');

exit($csvFile);

I have looped the data to build the CSV as your code would not produce the result you expect. I have also retrieved the file as a string before outputting - this is just a nicety to add a Content-Length header. I have also - and this is the important bit - called exit to output the data, to prevent any more code being executed any prevent and HTML after this code being output.

If you are using this code and still having a problem, then the code is not being called - you should check any if statements etc that this code is wrapped in.

萝莉病 2025-01-05 06:21:34

虽然已经晚了,但我们希望这能解除对某人的封锁。

使用 ob_end_clean() 清除页面上的其他缓冲区。

ob_end_clean(); 开始,以 exit; 结束

,这样,只有中间的输出才会写入文件。

This is way late but let's hope this unblocks someone.

Use ob_end_clean() to clean out other buffers on the page.

Start with ob_end_clean(); and end with exit;

This way, only the output in between will be written to the file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文