PHP 8.1.28 Released!

SplTempFileObject::__construct

(PHP 5 >= 5.1.2, PHP 7, PHP 8)

SplTempFileObject::__construct新しい一時ファイルオブジェクトを作成する

説明

public SplTempFileObject::__construct(int $maxMemory = 2 * 1024 * 1024)

新しい一時ファイルオブジェクトを作成します。

パラメータ

maxMemory

一時ファイルに使うメモリの最大量(バイト単位、デフォルトは 2 MB)。 一時ファイルがこのサイズを越える場合、ファイルはシステムの一時ディレクトリに移動させられます。

maxMemory が負の値である場合、 メモリだけが使われます。maxMemory がゼロの場合、メモリは使われません。

エラー / 例外

エラーが起きる場合 RuntimeException がスローされます。

例1 SplTempFileObject() の例

この例では読み書きできるメモリで一時ファイルが書き込まれます。

<?php
$temp
= new SplTempFileObject();
$temp->fwrite("これは最初の行で\n");
$temp->fwrite("そしてこれは 2 番目の行です。\n");
echo
"一時ファイルに " . $temp->ftell() . " バイトが書き込まれました。\n\n";

// 先頭に巻き戻し、書かれた内容を読み取る
$temp->rewind();
foreach (
$temp as $line) {
echo
$line;
}
?>

上の例の出力は、 たとえば以下のようになります。

一時ファイルに 47 バイト書き込まれました。

これは最初の行で
そしてこれは 2 番目の行です。

参考

add a note

User Contributed Notes 1 note

up
18
larry dot laski at gmail dot com
8 years ago
Noting that when the tmp file exceeds memory limitations and is written to the system temp directory, it is deleted upon completion of the script it was initially created in. At least that is what I have seen and wanted to document for others since it wasn't clear.
To Top