Create a php file of specified size
P粉221046425
P粉221046425 2023-10-26 14:03:01
0
2
399

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

P粉221046425
P粉221046425

reply all(2)
P粉039633152

You can use 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.

When executing:

$ php a.php

$ wc somefile.txt
  0   1 100 somefile.txt
$
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!