Skip to content
Advertisement

php create file with given size

How can i create in PHP a file with a given size (no matter the content)?

I have to create a file bigger than 1GB. Arround 4-10GB maximum

Advertisement

Answer

You can make use of fopen and 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.

On execution:

$ php a.php

$ wc somefile.txt
  0   1 100 somefile.txt
$ 
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement