Page 1 of 1

Create a File

Posted: Tue Apr 25, 2006 6:49 pm
by Maluendaster
How do I create a PHP file?? for example, i have a file called xxx.php with text in it, what i want to do is
delete that file with the unlink functin (i think), then create the same file with other text in it from a form...

EDIT :
I think i solved it, here's the code that worked :

Code: Select all

<?php
$header = "<?php include('header.php'); ?>";
$contenido = "<?php include('header.php'); ?> HOLA HOLA HOLA";
$archivo = "nosotros.php";
fopen($archivo, 'wb');
if (is_writable($archivo)) {
   if (!$handle = fopen($archivo, 'a')) {
         echo "No se puede abrir el archivo -> ($archivo)";
         exit;
   }

   if (fwrite($handle, $contenido) === FALSE) {
       echo "No se puede escribir la fila -> ($archivo)";
       exit;
   }
  
   echo "Actualizado! se agrego lo siguiente\n ($contenido)";
  
   fclose($handle);

} else {
   echo "El archivo $archivo no es de escritura!";
}
?>

Posted: Tue Apr 25, 2006 6:57 pm
by feyd
In one fell swoop:

Code: Select all

$fp = fopen($filename, 'wb');
fwrite() and fclose() will be used from there.

Posted: Tue Apr 25, 2006 6:59 pm
by Maluendaster
feyd wrote:In one fell swoop:

Code: Select all

$fp = fopen($filename, 'wb');
fwrite() and fclose() will be used from there.
What's the B for????

Posted: Tue Apr 25, 2006 7:01 pm
by feyd
binary mode, b or t (text mode) are generally required for fopen() to work correctly, or at all, on many Windows machines.

Posted: Tue Apr 25, 2006 7:02 pm
by Maluendaster
feyd wrote:binary mode, b or t (text mode) are generally required for fopen() to work correctly, or at all, on many Windows machines.
thank you.

Posted: Tue Apr 25, 2006 7:30 pm
by Maluendaster
is this code ok?? it works, but i need to write this at the start of the page.

Code: Select all

<?php $header ="include('header.php'); ?>
and this at the end :

Code: Select all

<?php $footer ="include('footer.php'); ?>
This is waht i have so far, but it's not working..

Code: Select all

<?php
$header = "<?php include('header.php'); ?>";
$contenido = $_POST['content']";
$archivo = "nosotros.php";
fopen($archivo, 'wb');
if (is_writable($archivo)) {
   if (!$handle = fopen($archivo, 'a')) {
         echo "No se puede abrir el archivo -> ($archivo)";
         exit;
   }

   if (fwrite($handle, $header, $contenido) === FALSE) {
       echo "No se puede escribir la fila -> ($filename)";
       exit;
   }
  
   echo "Actualizado! se agrego lo siguiente\n ($contenido)";
  
   fclose($handle);

} else {
   echo "El archivo $archivo no es de escritura!";
}
?>
If I remove the $header in

Code: Select all

if (fwrite($handle, $header, $contenido) === FALSE) {


works fine, buti need to insert the $header for the page to look fine.