问题描述
我编写了一个程序来生成大型.SQL文件,以快速填充大型数据库.我用PHP编写了脚本.当我开始编码时,我使用的是 fopen()和 fwrite().当文件太大时,程序会将控制权交还给外壳程序,并且文件将不完整.
I wrote a program to generate large .SQL files for quickly populating very large databases. I scripted it in PHP. When I started coding I was using fopen() and fwrite(). When files got too large the program would return control to the shell and the file would be incomplete.
不幸的是,我不确定太大"到底有多大.我认为可能约为4GB.
Unfortunately I'm not sure exactly how large is 'too large'. I think it may have been around 4GB.
要解决此问题,我将文件 echo 放入了stdout.像这样调用程序时,我将其重定向:
To solve this problem I had the file echo to stdout. I redirected it when I called the program like so:
[root@localhost]$ php generatesql.php > myfile.sql
那像魅力一样工作.我的输出文件最终大约是10GB.
Which worked like a charm. My output file ended up being about 10GB.
那么,我的问题是:就文件系统能够生成的文件大小而言, fopen()和 fwrite()是否受到文件系统的限制?如果是这样;这是PHP的限制吗?其他语言也会发生这种情况吗?
My question, then, is: Are fopen() and fwrite() limited by the file system in terms of how large a file they are capable of generating? If so; is this a limitation of PHP? Does this happen in other languages as well?
推荐答案
可能发生的是底层PHP构建为32位,并且无法处理大于4GB的文件指针-请参阅此.
What's probably occuring is the underlying PHP build is 32bit and can't handle file pointers >4GB - see this related question.
您的底层操作系统显然能够存储大文件,这就是为什么您能够将stdout重定向到大文件的原因.
Your underlying OS is obviously capable of storing large files, which why you're able to redirect stdout to a large file.
偶然地,SQL文件可能具有高度可压缩性,因此您可能要考虑使用 gzip fopen包装器以在编写文件时对其进行压缩.
Incidentally, an SQL file is likely to be highly compressible, so you might like to consider using the gzip fopen wrapper to compress the file as you write it.
$file = 'compress.zlib:///path/to/my/file.sql.gz';
$f = fopen($file, 'wb');
//just write as normal...
fwrite($f, 'CREATE TABLE foo (....)');
fclose($f);
您的转储将是原始大小的一小部分,您可以通过 zcat 进入SQL客户端,例如对于mysql
Your dump will be a fraction of the original size, and you can restore it simply piping the output from zcat into an SQL client, e.g. for mysql
zcat /path/to/my/file.sql.gz | mysql mydatabase
这篇关于fopen()是否受文件系统限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!