Hola. Hago entrega del ejercicio CU00836B del curso básico de programación web con PHP usando Notepad++ como editor. Función fopen (modo), fgets, fputs, fclose y feof.
Enlace:http://paramonso.byethost7.com/Ejercicio36(CU00836B).php (http://paramonso.byethost7.com/Ejercicio36(CU00836B).php)
Codigo HTML, PHP :
<!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:350px;
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>Ejercicio36(CU00836B).php</h1>
<h2>Leer y escribir archivos de texto con PHP.<br/>
Función fopen (modo), fgets, fputs, fclose y feof. </h2>
<?phpecho '<div><br/>';// Llamada a la funcion apartado a)$num =array(121,232,535.55 );$fp = fopen("datosEjercicio.txt", "w"); escribirTresNumeros($num,$fp);fclose($fp);// Llamada a la funcion apartado b)$fp = fopen("datosEjercicio.txt", "r");$resultado=obtenerSuma($fp,$TotalSuma);fclose($fp);echo "<p> Apartado b)<br/> El resultado de la suma es = ".$resultado."<br/><br/>Apartado c)<br/></p>";// Llamada a la funcion apartado c)$fp = fopen("datosEjercicio.txt", "r");$resultado =obtenerArrNum($fp,$Array); for($i=0;$i<count($resultado)-1;$i++) {echo "<p>El indice $i =".$resultado[$i].'</p>';}fclose($fp);// Llamada a la funcion apartado d)echo "<p><br/>Apartado d)</p>";$num =array(2,8,14 );$fp = fopen("datosEjercicio2.txt", "w"); escribirTresNumeros($num,$fp);fclose($fp);$fp = fopen("datosEjercicio2.txt", "r");$resultado=obtenerSuma($fp,$TotalSuma); Echo "<p>El resultado de la suma es = ".$resultado."</p>";rewind($fp );$res =obtenerArrNum($fp,$Array); for($i=0;$i<count($res)-1;$i++) {echo "<p>El indice $i =".$res[$i]."</p>";}fclose($fp);echo '<br/>';echo '</div>';//>>>>>>>>>>>>>>>>>>>>>>>>>>>> FUNCIONES <<<<<<<<<<<<<<<<<<<<<<<<//Funcion apartado a)function escribirTresNumeros($a,$Fichero){ foreach($a as $valor) { $n=$valor; fputs($Fichero,$n.chr(13).chr(10)); }}//Funcion apartado b)function obtenerSuma($a,$suma){ $m=0; while(!feof($a)) { $linea = fgets($a); $suma=$suma+$linea; } return $suma;}//Funcion apartado c)function obtenerArrNum($a,$matriz){ $m=0; while(!feof($a)) { $linea = fgets($a); $matriz[$m]=$linea; $m++; } return $matriz;}?>
<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>
Hasta luego ::)