I'm trying to make sure that when I press the letter S, the text file that I loaded will put an S at the end of the current line. That is, if I have pressed 5 times the S, the counter must be equal to 5, therefore it must be marked up to line 5 of the text file. How can I do it?
<form action="index.php" method="post" enctype="multipart/form-data">
<input type="file" name="archivo" />
<br>
<br>
<input type="submit" name="submit" value="Enviar" />
</form>
<?php
$ultimo_archivo;
if(isset($_POST['submit'])) {
if(is_uploaded_file($_FILES['archivo']['tmp_name'])) {
$ultimo_archivo = $_FILES['archivo']['name'];
move_uploaded_file($_FILES['archivo']['tmp_name'], $ultimo_archivo);
$lineas_archivo = file($ultimo_archivo);
?>
<script>
let cont = 0; // Pone el contador a 0;
const lineas = <?php echo json_encode($lineas_archivo); ?>;
<?php
foreach($lineas_archivo as &$l) {
$l = str_replace("\r\n", " S\r\n", $l);
}
fopen($ultimo_archivo, "w");
file_put_contents($ultimo_archivo, implode("", $lineas_archivo));
//Borra el archivo despues de leer todas las lineas
//unlink($ultimo_archivo);
//fopen('localhost/prueba/'.$ultimo_archivo, 'r');
?>
//Añade la primera linea
document.body.append(lineas[cont++]);
document.body.innerHTML += "<br>";
//Añade un evento cuando se pulse la letra S
addEventListener("keydown", function(event) {
if(event.key === "s" || event.key === "S") {
if(cont <= lineas.length-1) {
//Agrega la siguiente linea e incrementa el contador
//Modifica la linea y agrega una S al final
linea_actual = lineas[cont++];
document.body.innerHTML += "<br>";
document.body.append(linea_actual);
}
}
});
</script>
<?php
}
}
?>