简体   繁体   English

如何通过php在x86系统上制作具有所需大小的空文件

[英]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. 我通过php在Windows Server 2003 x86上制作一个具有所需大小的空文件。 A file larger than 2GB example 2.67Gb - 2870348740 bytes. 大于2GB的文件,例如2.67Gb-2870348740字节。

I use this code but makes a file with size of 1.89GB - 2040109466 bytes. 我使用此代码,但制作的文件大小为1.89GB-2040109466字节。

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);

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 您也可以在x86系统上使用

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

I extended @gafreax solution and maked this code and problem solved. 我扩展了@gafreax解决方案,并使此代码得以解决。

thanks to @gafreax . 感谢@gafreax。

function makeFile($size,$dest){
    $eachpart = 100 * 1024*1024; // 100Mb
    $parts = ceil($size/$eachpart);
    for ($i=0; $i<$parts; $i++){
        if ($i == $parts-1) $offset = $size - (($parts-1)*$eachpart);
        else $offset = $eachpart;

        file_put_contents($dest, str_repeat("\0",$offset),FILE_APPEND);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM