Buenas a todo el mundo, en este aporte os dejo un script en php que puede ser utilizado a modo de controlador para cualquier aplicación, nos permite recuperar los datos get o post y almacenar su contenido en un un archivo.txt que luego también podemos leer y recuperar los datos.
Tambien utilizamos la funcion parse_str($string,$array) donde recuperamos los datos de cadena a matriz nuevamente.
También se puede utilizar la funciones Guarder y Leer Datos directamente pasando los argumento necesarios que están de forma estatica en las variables de entorno.
Espero que lo disfruten cómo lo disfruto yo para muchos proyectos.
<?php
// create by Toni Domenech.
echo 'La Versión de PHP es:'.PHP_VERSION;
/*
// $met = comandos para abrir un fichero de una formas determinada:
"r" - Read only. Starts at the beginning of the file
"r+" - Read/Write. Starts at the beginning of the file
"w" - Write only. Opens and truncates the file; or creates a new file if it doesn't exist. Place file pointer at the beginning of the file
"w+" - Read/Write. Opens and truncates the file; or creates a new file if it doesn't exist. Place file pointer at the beginning of the file
"a" - Write only. Opens and writes to the end of the file or creates a new file if it doesn't exist
"a+" - Read/Write. Preserves file content by writing to the end of the file
"x" - Write only. Creates a new file. Returns FALSE and an error if file already exists
"x+" - Read/Write. Creates a new file. Returns FALSE and an error if file already exists
"c" - Write only. Opens the file; or creates a new file if it doesn't exist. Place file pointer at the beginning of the file
"c+" - Read/Write. Opens the file; or creates a new file if it doesn't exist. Place file pointer at the beginning of the file
"e" - Only available in PHP compiled on POSIX.1-2008 conform systems.
*/
//declaracion de variables de entorno
$rut = 'datos.txt';
$met = "a";
$msn = "nombre=xxx&edad=18&casa=roja";
if($_GET){
$datos = $_SERVER['QUERY_STRING'];
$rut = 'datos.txt';
$met = "a";
GrabarFichero($rut,$met,$datos);
}
if($_POST){
$datos = $_POST;
$rut = 'datos.txt';
$met = "a";
GrabarFichero($rut,$met,$datos);
}
function GrabarFichero($rut,$met,$msn){
$files = fopen($rut,$met);
//fwrite($files,$msn.PHP_EOL); //PHP_EOL cambio de linea
fwrite($files,$msn.'&');
fclose($files);
}
function LeerFichero($rut,$met){
$files = fopen($rut,$met);
$i=0;
while(!feof($files)){
$datos[$i] = fgets($files). "<br>";
$i++;
}
fclose($files);
return $datos;
}
//GrabarFichero($rut,$met,$msn);
$valor = LeerFichero($rut,"r");
// Recuperamos el valor del txt y lo ajustamos para la función parse_str
parse_str(implode("",$valor), $datos);
// finalmente imprimimo o utilizamos.
print_r($datos);
/// Funciones Jshon
$misdatos = array("Cerverza","Cocacola");
$json = json_encode($misdatos);
print_r($json).'<br>';
print_r(json_decode($json));
?>