如何在PHP中创建具有给定大小(无论内容如何)的文件?
我必须创建一个大于1 1GB的文件。最大4-10左右
发布于 2010-08-31 19:12:20
您可以使用fopen
和fseek
define('SIZE',100); // size of the file to be created.
$fp = fopen('somefile.txt', 'w'); // open in write mode.
fseek($fp, SIZE-1,SEEK_CUR); // seek to SIZE-1
fwrite($fp,'a'); // write a dummy char at SIZE position
fclose($fp); // close the file.
执行时:
$ php a.php
$ wc somefile.txt
0 1 100 somefile.txt
$
发布于 2010-08-31 19:17:27
如果文件的内容无关紧要,那么只需填充它-但请确保您生成的变量不会太大而无法保存在内存中:
<?php
$fh = fopen("somefile", 'w');
$size = 1024 * 1024 * 10; // 10mb
$chunk = 1024;
while ($size > 0) {
fputs($fh, str_pad('', min($chunk,$size)));
$size -= $chunk;
}
fclose($fh);
如果文件必须是其他东西可读的-那么你如何做取决于需要读取它的另一个东西。
结果表明,C.
发布于 2020-10-12 07:54:55
晚了,但它真的比其他答案更容易。
$size = 100;
$fp = fopen('foo.dat',"w+");
fwrite($fp,str_repeat(' ',$size),$size);
w+将创建该文件,如果该文件已经存在,则将其覆盖。
对于非常大的文件,我通常会作弊:
`truncate -s 10g foo.dat`;
https://stackoverflow.com/questions/3608383
复制相似问题