how to put php or html in fwrite?

0

I am creating a mini system to create a folder with files inside. but I want the files to have the html and php code inside when it's created.

The problem is that I can not find information to help me? Could someone help me?

Code:

<?php  
    $dirname = $_POST["name"];  
    $filename = "{$dirname}";  
    
    if (file_exists($filename)) {  
        echo "El nombre de usuario {$dirname} Ya existe";  
    } else {  
        mkdir("users/{$dirname}", 0777);  
	$content = "Nombre:".$_POST["name"]." Direccion:".$_POST["address"]; 
	$fp = fopen("users/{$dirname}/index.php", "w");
	$fp = fopen("users/{$dirname}/archivo.php", "w");
	$fp = fopen("users/{$dirname}/configuracion.php", "w");
	$fp = fopen("users/{$dirname}/configuracion_cambiar.php", "w");
	$fp = fopen("users/{$dirname}/configuracion_eliminararchivos.php", "w");
	$fp = fopen("users/{$dirname}/configuracion_fotoperfil.php", "w");
	$fp = fopen("users/{$dirname}/eliminararchivos.php", "w");
	$fp = fopen("users/{$dirname}/eliminarcuenta.php", "w");
	$fp = fopen("users/{$dirname}/eliminarfoto.php", "w");
	$fp = fopen("users/{$dirname}/enviado.php", "w");
	$fp = fopen("users/{$dirname}/login.php", "w");
	$fp = fopen("users/{$dirname}/panel.php", "w");
	$fp = fopen("users/{$dirname}/perfilcorrecto.php", "w");
	$fp = fopen("users/{$dirname}/salir.php", "w");
	$fp = fopen("users/{$dirname}/subir", "w");
    $fp = fopen("users/{$dirname}/validar.php", "w");
	fwrite($fp,$content); 
	fclose($fp); 
        echo "Tu cuenta {$dirname} Fue creado correctamente.";  
    }


?>  

 <?php
$fp = fopen("{users/$dirname}/index.php", "w") or die("Error al intentar abrir el archivo!");
$txt = "test\n";
fwrite($fp, $txt);
fclose($fp);
?> 
    
asked by danielmeza 30.12.2018 в 21:09
source

1 answer

0

In the following example I create a PHP script from the current one and execute it:

<?php

    ////////////////////////////////////////////////////////////////////
    // Create a PHP file to print a message, if it don't exist
    //
    $file_path = "xxx.php";
    if (! file_exists($file_path)) {
        echo "El archivo \"$file_path\" no existe. Creando... <br>";

        if ($archivo = fopen($file_path, "a")) {
            $cotent = "<?php" . PHP_EOL
                . "echo 'Hello world <br>';" . PHP_EOL;

            if (fwrite($archivo, $cotent)) {
                echo "Cotenido guardado <br>";
            } else {
                echo "Error guardando contenido <br>";
            }

            fclose($archivo);
        }
    }


    ////////////////////////////////////////////////////////////////////
    // Load and run the PHP file created
    //
    if (file_exists($file_path)) {
        require $file_path;
    } else {
        echo "El archivo no existe";
    }

The result of the first execution will be:

El archivo "xxx.php" no existe. Creando... 
Cotenido guardado 
Hello world 

and the following:

Hello world


  

Edited

To do this in a new directory you will have to create it and give it read and write permissions.

    
answered by 30.12.2018 / 21:56
source