Estoy usando el siguiente código PHP para escribir en mi wall.txt
expediente. Funciona bien pero el problema es que wall.txt
el tamaño del archivo sigue aumentando. quiero recortar el wall.txt
archivo una vez que se agreguen los nuevos datos después de 3 párrafos. Se eliminará el párrafo anterior mientras que uno nuevo se agregará
Aquí está el archivo php
<?php
$joke = $_POST["AndroidString"];
$complexString = ($joke . "|" . date("l") . ", " . date("jS of F Y h:i:s A"));
$endLineStericks = "****";
$registrationFile = fopen("wall.txt", "a") or die("Unable to open file!");
fwrite($registrationFile, $complexString);
fwrite($registrationFile, "n");
fwrite($registrationFile, $endLineStericks);
fwrite($registrationFile, "n");
fclose($registrationFile);
?>
aquí está el archivo de texto
Boy: ¡El director es tan tonto! / -% / Girl: ¿Sabes quién soy? / -% / Boy: No ... / -% / Girl: ¡Soy la hija del director! / -% / Boy: ¿Sabes quién Estoy? / -% / Girl: No ... / -% / Boy: ¡Bien! Se alejaViernes, 05:23:04 pm **** Niño: llama al 911 ¿Hola? ¡Necesito tu ayuda!/ -% / 911: Muy bien, ¿qué es? / -% / Boy: ¡Dos chicas se pelean por mí! / -% / 911: Entonces, ¿cuál es tu ¿Emergencia? / -% / Boy: el feo está ganando. | Viernes, 05:36:19 pm **** Lo más cerca que he estado de una dieta este año es borrar las búsquedas de alimentos del historial de mi navegador./-%//-%/Lollzzzzz: p | Friday, 05:44:35 pm
Respuestas
0 para la respuesta № 1Solución utilizando file
y array_slice
funciones:
define("WALL", "wall.txt");
$joke = $_POST["AndroidString"];
fopen(WALL, "a") || die("Failed to open file!");
$contents = file(WALL); // gets file contents as array of strings(lines)
$complexString = ($joke . "|" . date("l") . ", " . date("jS of F Y h:i:s A")). PHP_EOL;
$endLineStericks = "****" . PHP_EOL;
$contents = array_merge($contents, [$complexString, $endLineStericks]);
// if there were at least three records(including end stricks) in wall.txt
if (count($contents) > 6) {
$contents = array_slice($contents, -6); // getting last three records
}
file_put_contents(WALL, implode("", $contents));