Update 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

Update File....

Post by Maluendaster »

I need to edit a file, by adding text at the end, for example...

1
2
3
4
5

If i want to add the number six, it should write under 5, but it writes like this..

1 2 3 4 5

Don't know what I'm doing wrong...

This is the Code

actu_cat.php :

Code: Select all

<?php
include ('config.php');
include ('admin.php');
?>


<div align="center"><font color="#999999" size="3" face="Verdana, Arial, Helvetica, sans-serif">Agregar
Nueva Categoria</font></div>
<form name="form1" method="post" action="proc_actu_cat.php">
  <p>
    <center>
      <input name="cat" type="text" id="cat">
      <br>
      <input type="submit" name="Submit" value="Agregar Categoria">
</center>
</p>
</form>
proc_actu_cat.php :( I know it has something to do with this fopen($archivo, 'a+'); )

Code: Select all

<?
include('config.php');
include('admin.php');
$form = $_POST['cat'];
$contenido = "$form <br>";
$archivo = "cat.php";
fopen($archivo, 'a+');
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 "<center><font color=white size=5>Actualizado!</font>";
  
   fclose($handle);

} else {
   echo "El archivo $archivo no es de escritura!";
}
?>
User avatar
ambivalent
Forum Contributor
Posts: 173
Joined: Thu Apr 14, 2005 8:58 pm
Location: Toronto, ON

Post by ambivalent »

You need to write a CRLF to the file, not <br>

Code: Select all

$contenido = "$form \r\n";
Maluendaster
Forum Contributor
Posts: 124
Joined: Fri Feb 25, 2005 1:14 pm

Post by Maluendaster »

ambivalent wrote:You need to write a CRLF to the file, not <br>

Code: Select all

$contenido = "$form \r\n";
thank you! it worked... btw , what is a CRLF??? I'm learning... :D Another question, what's the $handle Variable for??? , i copy and edit this from php.net....
Maluendaster
Forum Contributor
Posts: 124
Joined: Fri Feb 25, 2005 1:14 pm

Post by Maluendaster »

and what if i want to edit the file to delete a line, the file is like this...
1
2
3
4
5

but i want to delete, for example, the number 3 ,and leave the file like this
1
2
4
5

What should I do????
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

pretty much, you have to rewrite the entire file.
Maluendaster
Forum Contributor
Posts: 124
Joined: Fri Feb 25, 2005 1:14 pm

Post by Maluendaster »

feyd wrote:pretty much, you have to rewrite the entire file.
then i guess that's not an option, don't know how to do that. :oops:
Post Reply