Search code examples
phpmakefilex86fseek

how make empty file with desired size on x86 systems by php


I wnat to make an empty file with desired size on windows server 2003 x86 by php. A file larger than 2GB example 2.67Gb - 2870348740 bytes.

I use this code but makes a file with size of 1.89GB - 2040109466 bytes.

What I must do now ?

$size = 2870348740;
$fp = fopen('fileLocation/test.x', 'wb'); // open in write mode.
$eachpart = floor(1.9 * 1024*1024*1024); // 1.9 Gbyte to byte
$parts = ceil($size/$eachpart);
for ($i=0; $i<$parts; $i++){
    if ($i == $parts-1) $offset = $size - (($parts-1)*$eachpart) -1;
    else $offset = $eachpart;

    fseek($fp, $offset ,SEEK_CUR); // seek from current position
}
fwrite($fp,0); // write a dummy char at SIZE position
fclose($fp);

Solution

  • You can simply do this

    file_put_contents('mybigfatfile.txt', str_repeat("\0",2*1024*1024*1024));
    

    Update

    you can build the string also on x86 system with

    $kb = str_repeat("\0",1024);
    $mb = str_repeat($kb,1024);
    $gb = str_repeat($mb,1024);
    file_put_contents('mybigfatfile.txt', str_repeat($gb,2));