CakeFest 2024: The Official CakePHP Conference

SplTempFileObject::__construct

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

SplTempFileObject::__constructConstruir un nuevo objeto de fichero temporal

Descripción

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

Construir un nuevo objeto de fichero temporal.

Parámetros

maxMemory

La cantidad máxima de memoria (en bytes, por omisión es 2 MB) para el fichero temporal a usar. Su el fichero temporal supera este tamaño, Este será movido a un archivo en el directorio temporal del sistema.

Si maxMemory es negativo, se usará memoria. Si maxMemory es cero, no se usará memoria.

Errores/Excepciones

Lanza una RuntimeException si un error ocurre.

Ejemplos

Ejemplo #1 Ejemplo de SplTempFileObject()

Este ejemplo escribe un fichero temporal en la memoria mientras se puede escribir y leer en este.

<?php
$temp
= new SplTempFileObject();
$temp->fwrite("Esta es la primera línea\n");
$temp->fwrite("Y esta es la segunda.\n");
echo
"Escrito " . $temp->ftell() . " bytes al fichero temporal.\n\n";

// Rebobina y lee lo que fué escrito
$temp->rewind();
foreach (
$temp as $line) {
echo
$line;
}
?>

El resultado del ejemplo sería algo similar a:

Escrito 47 bytes al fichero temporal.

Esta es la primera línea
Y esta es la segunda.

Ver también

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