附加模式下 fopen 的 PHP 内存使用情况

发布于 2024-11-18 00:09:33 字数 335 浏览 1 评论 0原文

我有一个自定义 CakePHP 购物车应用程序,我试图在其中创建一个 CSV 文件,其中包含每笔交易的一行数据。当 PHP 通过编译 MySql 数据库中的相关数据来立即创建 CSV 文件时,我遇到了内存问题。目前,CSV 文件包含大约 200 行数据。

或者,我考虑过通过在每次进行事务时将一行数据附加到文件中来逐步创建 CSV: fopen($mFile.csv, 'a');

我的开发人员说,当 CSV 文件变得太大时,我仍然会遇到这种方法的内存问题,因为 PHP 会将整个文件读入内存。是这样吗?当使用追加模式时,PHP 会尝试将整个文件读入内存吗?如果是这样,您能推荐更好的方法吗?

提前致谢, 本

I have a custom CakePHP shopping cart application where I’m trying to create a CSV file that contains a row of data for each transaction. I’m running into memory problems when having PHP create the CSV file at once by compiling the relevant data in the MySql databse. Currently the CSV file contains about 200 rows of data.

Alternatively, I’ve considered creating the CSV in a piecemeal process by appending a row of data to the file every time a transaction is made using: fopen($mFile.csv, 'a');

My developers are saying that I will still run into memory issues with this approach when the CSV file gets too large as PHP will read the whole file into memory. Is this the case? When using the append mode will PHP attempt to read the whole file into memory? If so, can you recommend a better approach?

Thanks in advance,
Ben

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

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

发布评论

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

评论(2

梦魇绽荼蘼 2024-11-25 00:09:33

我运行了以下脚本几分钟,并生成了一个 1.4GB 的文件,远远超出了我的 php 内存限制。我也毫无问题地读取文件。如果您遇到内存问题,则可能是其他原因造成的。

$fp = fopen("big_file.csv","a");

for($i = 0; $i < 100000000; $i++)
{
    fputcsv($fp , array("val1","val2","val3","val4","val5","val6","val7","val8","val9"));
}

I ran the following script for a few minutes, and generated a 1.4gb file, well over my php memory limit. I also read from the file without issue. If you are running into memory issues it is probably something else that is causing the problem.

$fp = fopen("big_file.csv","a");

for($i = 0; $i < 100000000; $i++)
{
    fputcsv($fp , array("val1","val2","val3","val4","val5","val6","val7","val8","val9"));
}
入怼 2024-11-25 00:09:33

你不能像这样从数据库导出吗:

SELECT list_fields INTO OUTFILE '/tmp/result.text'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM test_table; 

can't you just export from the db like so:

SELECT list_fields INTO OUTFILE '/tmp/result.text'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM test_table; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文