Escribir en un fichero:
if (!$f = @fopen("pepe.txt", "w")) {
die("No he podido abrir el fichero: " . error_get_last()['message']);
}
for ($i = 1; $i <= 10; $i++) {
fwrite($f, "$i x 5=".(5*$i)."\r\n");
}
fclose($f);
if (!$f = @fopen("pepe.txt", "a")) {
die("No he podido abrir el fichero: " . error_get_last()['message']);
}
for ($i = 1; $i <= 10; $i++) {
fwrite($f, "$i x 7=".(7*$i)."\r\n");
}
fclose($f);
Leer un fichero:
$f = @fopen("pepe.txt", "r") or die("No he podido abrir el fichero");
//Lee una línea
while (!feof($f)) {
echo "<h2>".fgets($f)."</h2>";
}
fclose($f);
Leer directamente un fichero:
$cadena= file_get_contents("pepe.txt");
$tabla=file("pepe.txt");
echo $cadena;
print_r($tabla);