Create a File

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Maluendaster
Forum Contributor
Posts: 124
Joined: Fri Feb 25, 2005 1:14 pm

Create a File

Post 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!";
}
?>
Last edited by Maluendaster on Tue Apr 25, 2006 8:09 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

In one fell swoop:

Code: Select all

$fp = fopen($filename, 'wb');
fwrite() and fclose() will be used from there.
Maluendaster
Forum Contributor
Posts: 124
Joined: Fri Feb 25, 2005 1:14 pm

Post 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????
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

binary mode, b or t (text mode) are generally required for fopen() to work correctly, or at all, on many Windows machines.
Maluendaster
Forum Contributor
Posts: 124
Joined: Fri Feb 25, 2005 1:14 pm

Post 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.
Maluendaster
Forum Contributor
Posts: 124
Joined: Fri Feb 25, 2005 1:14 pm

Post 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.
Post Reply