How can I create a file with data inside and if it exists replace it with php

0

I want it when a url arrives:

  

? page = home & amp = change = user1

and put it in $ _GET variable ['']

$Cambiar_recibido = $_GET['cambiar'];

//Imprimir esa varible 



 $usuario = $Cambiar_recibido;

//Crear el archivo y si ya existe uno igual que lo remplaze, por favor lo necesito o de que otra forma puedo hacerlo?

for what I discovered is created with this

$nuevoarchivo = fopen($titulo, "w+"); 
fwrite($nuevoarchivo,"texto qe contiene el nuevo archivo"); 
fclose($nuevoarchivo);  

pero no se como ponerlo en un directorio especifico;
    
asked by Cris GO 28.04.2018 в 23:51
source

1 answer

1

You'd better filter the request $ _GET before executing any request and for your problem you can read a little visit read but here is a simple example

$dir = "mydir";
$file = "myfile.php";
$contenido = "<?php // this is just a comment ?>";
$replazo = "<?php  echo 'DONE'; ?>";
    if (!file_exists($dir)) {
       mkdir($dir, 0777, true);
       return file_put_contents($dir.'/'.$file, $contenido, 0);
    }else{

       $filecontent = file_get_contents($file);
       $pos = strpos($filecontent, '?>');
       $filecontent = substr($filecontent, 0, 
       $pos)."\r\n".$replazo."\r\n".substr($filecontent, $pos);
       file_put_contents($dir.'/'.$file, $filecontent);
       include($dir.'/'.$file);
}

The same question has already been asked in here

    
answered by 29.04.2018 в 01:09