附加模式下 fopen 的 PHP 内存使用情况
我有一个自定义 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我运行了以下脚本几分钟,并生成了一个 1.4GB 的文件,远远超出了我的 php 内存限制。我也毫无问题地读取文件。如果您遇到内存问题,则可能是其他原因造成的。
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.
你不能像这样从数据库导出吗:
can't you just export from the db like so: