CakeFest 2024: The Official CakePHP Conference

SplTempFileObject::__construct

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

SplTempFileObject::__constructConstruit un nouvel objet représentant un fichier temporaire

Description

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

Construit un nouvel objet représentant un fichier temporaire.

Liste de paramètres

maxMemory

La mémoire maximale (en octets, par défaut, 2 Mo) à utiliser pour le fichier temporaire. Si un fichier temporaire dépasse cette taille, il sera déplacé sur le système de fichiers, dans le dossier des fichiers temporaires.

Si maxMemory est négatif, seule la mémoire sera utilisée. Si maxMemory vaut zéro, la mémoire ne sera pas utilisée.

Erreurs / Exceptions

Lance un exception RuntimeException si une erreur survient.

Exemples

Exemple #1 Exemple avec SplTempFileObject()

Cet exemple écrit un fichier temporaire en mémoire dans lequel nous allons pouvoir écrire et lire des données.

<?php
$temp
= new SplTempFileObject();
$temp->fwrite("This is the first line\n");
$temp->fwrite("And this is the second.\n");
echo
$temp->ftell() . " octets écrits dans le fichier temporaire.\n\n";

// Rewind and read what was written
$temp->rewind();
foreach (
$temp as $line) {
echo
$line;
}
?>

Résultat de l'exemple ci-dessus est similaire à :

47 octets écrits dans le fichier temporaire.

This is the first line
And this is the second.

Voir aussi

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