84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
How to create a file of a given size (regardless of content) in PHP?
I have to create a file larger than 1GB. Maximum approximately 4-10GB
If the contents of the file are not relevant, just populate it - but make sure you don't generate variables that are too large to hold in memory:
<?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);
If the file must be read by something else - then how you do it depends on what else needs to read it.
C.
You can use fopen and fseek
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.
When executing:
$ php a.php $ wc somefile.txt 0 1 100 somefile.txt $
If the contents of the file are not relevant, just populate it - but make sure you don't generate variables that are too large to hold in memory:
If the file must be read by something else - then how you do it depends on what else needs to read it.
C.
You can use
fopen
andfseek
When executing: