php创建给定大小的文件

Mik*_*ike 7 php createfile

如何在PHP中创建具有给定大小的文件(无论内容)?

我必须创建一个大于1GB的文件.最大4-10GB

cod*_*ict 10

你可以利用fopenfseek

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.
Run Code Online (Sandbox Code Playgroud)

执行时:

$ php a.php

$ wc somefile.txt
  0   1 100 somefile.txt
$ 
Run Code Online (Sandbox Code Playgroud)