Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP script to generate a file with random data of given name and size?

Tags:

file

php

random

Does anyone know of one? I need to test some upload/download scripts and need some really large files generated. I was going to integrate the test utility with my debug script.

like image 602
stormist Avatar asked Apr 12 '10 16:04

stormist


2 Answers

generate_file() from "Marco Demaio" is not memory friendly so I created file_rand().

function file_rand($filename, $filesize) {
    if ($h = fopen($filename, 'w')) {
        if ($filesize > 1024) {
            for ($i = 0; $i < floor($filesize / 1024); $i++) {
                fwrite($h, bin2hex(openssl_random_pseudo_bytes(511)) . PHP_EOL);
            }
            $filesize = $filesize - (1024 * $i);
        }
        $mod = $filesize % 2;
        fwrite($h, bin2hex(openssl_random_pseudo_bytes(($filesize - $mod) / 2)));
        if ($mod) {
            fwrite($h, substr(uniqid(), 0, 1));
        }
        fclose($h);
        umask(0000);
        chmod($filename, 0644);
    }
}

As you can see linebreaks are added every 1024 bytes to avoid problems with functions that are limited to 1024-9999 bytes. e.g. fgets() with <= PHP 4.3. And it makes it easier to open the file with an text editor having the same issue with super long lines.

like image 62
mgutt Avatar answered Oct 18 '22 11:10

mgutt


generate_file() from @Marco Demaio caused this below when generating 4GB file.

Warning: str_repeat(): Result is too big, maximum 2147483647 allowed in /home/xxx/test_suite/handler.php on line 38

I found below function from php.net and it's working like charm. I have tested it upto

17.6 TB (see update below)

in less than 3 seconds.

function CreatFileDummy($file_name,$size = 90294967296 ) {   
// 32bits 4 294 967 296 bytes MAX Size
    $f = fopen('dummy/'.$file_name, 'wb');
    if($size >= 1000000000)  {
        $z = ($size / 1000000000);       
        if (is_float($z))  {
            $z = round($z,0);
            fseek($f, ( $size - ($z * 1000000000) -1 ), SEEK_END);
            fwrite($f, "\0");
        }       
        while(--$z > -1) {
            fseek($f, 999999999, SEEK_END);
            fwrite($f, "\0");
        }
    }
    else {
        fseek($f, $size - 1, SEEK_END);
        fwrite($f, "\0");
    }
    fclose($f);

return true;
}

Update:

I was trying to hit 120TB, 1200 TB and more but filesize was limited to 17.6 TB. After some googling I found that it is max_volume_size for ReiserFS file system which was on my server. May be PHP can handle 1200TB also in just few seconds. :)

like image 33
shyammakwana.me Avatar answered Oct 18 '22 11:10

shyammakwana.me