php - 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);
}
解决方案
推荐阅读
- python - CuPY: Not seeing kernel concurrency
- java - Android Context.startActivity(Intent) 抛出运行时异常
- amazon-web-services - AWS 教育账户支持是否在两个 vpc 之间创建对等连接?
- python - 求解 Haversine 公式的分量时,sympy 挂起
- angular - Router.link 将最初导航到组件,但不会渲染另一个
- c++ - 重定向 libpng 警告 C++
- php - html 表单中的表单操作字段 - 不重定向到正确的 php 代码
- python - Skimage:如何在阈值图像的顶部底部和侧面添加更多黑色?
- c# - Azure C# v2 函数:数据未插入 Azure 表存储
- webpack - Webpack HMR 浏览器不输出 hot-update.json