Heim  >  Fragen und Antworten  >  Hauptteil

Erstellen Sie eine PHP-Datei mit der angegebenen Größe

Wie erstelle ich in PHP eine Datei einer bestimmten Größe (unabhängig vom Inhalt)?

Ich muss eine Datei erstellen, die größer als 1 GB ist. Maximal etwa 4-10 GB

P粉221046425P粉221046425333 Tage vor549

Antworte allen(2)Ich werde antworten

  • P粉561323975

    P粉5613239752023-10-27 11:36:14

    如果文件的内容不相关,则只需填充它 - 但请确保不会生成太大而无法保存在内存中的变量:

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

    如果文件必须被其他东西读取 - 那么你如何做到这一点取决于需要读取它的其他东西。

    C.

    Antwort
    0
  • P粉039633152

    P粉0396331522023-10-27 09:16:19

    您可以使用fopenfseek

    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.

    执行时:

    $ php a.php
    
    $ wc somefile.txt
      0   1 100 somefile.txt
    $

    Antwort
    0
  • StornierenAntwort