Hola. Hago la entrega del Ejercicio CU00837B del curso de PHP BASICO desde cero.
fwrite PHP. Lectura y escritura fichero txt. Modo acceso. PHP_EOL salto de línea.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>PHP Desde cero aprenderaprogramar.com</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta name="generator" content="Geany 1.27" />
<style type="text/css">
body{ background-color:yellow; color:white;}
h1, h2{
text-align:center;
color:green;
margin:0;
padding:0;
}
div{
text-align:left;
background-color:black;
height:700px;
width:500px;
padding:10px;
margin:10px auto;
}
p{
text-align:center;
font-weight: bold;
font-size:20px;
padding:1px;
margin:0px auto;
}
</style>
</head>
<body>
<h1>Ejercicio37(CU00837B).php</h1>
<h2>fwrite PHP. Lectura y escritura fichero txt.<br/>
Modo acceso. PHP_EOL salto de línea. Ejercicio ejemplo (CU00837B) </h2>
<?phpecho '<div><br/>';// Llamada a la funcion apartado a)$num =array(5, 9, 3, 22);$ModOpen="sobreescribir"; escribirNumerosMod($num,$ModOpen); // Llamada a la funcion apartado b)echo "<p> Apartado b)<br/> Los Numeros guardados son<p/>";$fichero= fopen("datosEjercicio3.txt", "r");leerContenidoFichero($fichero);fclose($fichero);echo "<br/>";// Llamadas a las funciones apartado c)echo "<p> Apartado c)<br/> Los Numeros guardados son<p/>";// Llamar escribir 2,8,14$num =array(2,8,14);$ModOpen="sobreescribir";escribirNumerosMod($num,$ModOpen); //Llamar a Leer y presentar por pantalla contenido de fichero$fichero= fopen("datosEjercicio3.txt", "r");leerContenidoFichero($fichero);fclose($fichero);echo "<br/><p>Ampliacion del fichero </p>";//Llamar ampliar contenido de fichero 33,11,16$num =array(33,11,16);$ModOpen="añadir";escribirNumerosMod($num,$ModOpen); //Llamar a Leer y presentar por pantalla contenido de fichero$fichero= fopen("datosEjercicio3.txt", "r");leerContenidoFichero($fichero);fclose($fichero);echo "<br/>";//Llamar sobreescribir fichero 4,99,12echo "<br/><p>Sobreescribir el fichero </p>";$num =array(4,99,12);$ModOpen="sobreescribir";escribirNumerosMod($num,$ModOpen); //Llamar a Leer y presentar por pantalla contenido de fichero$fichero= fopen("datosEjercicio3.txt", "r");leerContenidoFichero($fichero);fclose($fichero);echo '<br/>';echo '</div>';//>>>>>>>>>>>>>>>>>>>>>>>>>>>> FUNCIONES <<<<<<<<<<<<<<<<<<<<<<<<//Funcion escribir en el fichero modo write o appendfunction escribirNumerosMod($Numero,$Modo){ If ($Modo=="sobreescribir"){$Modo="w";} else if($Modo=="añadir"){$Modo="a";} if ($Modo=="w" or $Modo=="a") { $Fichero = fopen("datosEjercicio3.txt", $Modo); foreach($Numero as $valor) { $PutNum=$valor; fwrite($Fichero,$PutNum.PHP_EOL); } } fclose($Fichero);}//Funcion Leer el Contenido del Ficherofunction leerContenidoFichero($fs){ $m=0; $linea = fgets($fs); while(!feof($fs) and $linea<>"") { Echo "<p>La linea $m contiene el Numero :".$linea."</p>"; $m++; $linea = fgets($fs); }} ?>
<p>
<a href="http://validator.w3.org/check?uri=referer"><img
src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0 Strict" height="31" width="88" /></a>
</p>
</body>
</html>
Gracias.
;D ;)