首页 > 解决方案 > fwrite 在 foreach 中不起作用

问题描述

我正在使用以下代码获取链接列表:

$links=htmlspecialchars($_POST['links']);
$ids = explode("\n", str_replace("\r", "", $links));
$ids=array_filter($ids);

当我像这样运行一个简单的 foreach 时:

$csv=fopen("php://output", "w");
foreach ($ids as $url)
{
    $line='';
$line="test";
fwrite($csv, $line);
}

header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename="data.csv";');
fpassthru($csv); //Generate downloadabñe CSV
fclose($csv);

它不起作用。但是,当我像这样手动构建 $ids 时:

$ids=array();
$ids[0]='example';

该代码有效。该代码在本地有效,但不适用于我们购买的新主机。

我能做些什么?

PS:如果你能推荐一个 PHP 主机,那就太好了。我目前的托管服务提供商遇到了很多错误。

PS:对于问的人,这里有完整的代码。它在 CSV 上写入变量“hline”,而不是变量行:

ini_set('memory_limit', '-1');
require('simple_html_dom.php');
error_reporting(E_ALL); //Error reporting

//Set up csv
$hline='Home Depot URL'.','.'Home Depot Reviews'.','.'Lowes URL'.','.'Best Seller Title'.','.'Sales In Past Two Weeks'.',';
$hline.='Top Seller ID'.','.'eBay Stars Reviews'.PHP_EOL;
$csv=fopen("php://output", "w"); 
fwrite($csv, $hline);

//Get variables from form
$links=htmlspecialchars($_POST['links']);
$ids = explode("\n", str_replace("\r", "", $links));
$ids=array_filter($ids);

if (isset($_POST['snag'])) {
$snag=htmlspecialchars($_POST['snag']); 
}

if (isset($_POST['lowes'])) {
$lowes=htmlspecialchars($_POST['lowes']);
}

$markup0=htmlspecialchars($_POST['markup']);
$markup20=htmlspecialchars($_POST['markup2']);
$soldn=htmlspecialchars($_POST['sold']);

//Start not working foreach

foreach ($ids as $url)
{
    $line='';
$line="test";
fwrite($csv, $line);
}

标签: phpdebugging

解决方案


推荐阅读