PHP: Retrieve/modify data from a text file

Solved
ced29 -  
R4f Posted messages 441 Status Membre -
```php
<?php
// Ouverture du fichier en mode lecture et écriture
$fp = fopen("data.txt", "r+");

// Lecture des données existantes
fseek($fp, 1085);
$nomterminal = fgets($fp, 7);
fseek($fp, 153);
$adrip = fgets($fp, 255);

// Si le formulaire est soumis
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Récupération des valeurs du formulaire
$nomterminal = $_POST['nomterminal'];
$adrip = $_POST['adrip'];

// Réécriture des données dans le fichier
fseek($fp, 1085);
fwrite($fp, $nomterminal);
fseek($fp, 153);
fwrite($fp, $adrip);

// Optionnel: pour s'assurer que les données sont écrites, on pourrait faire un fflush
fflush($fp);
}

// Fermeture du fichier
fclose($fp);
?>

Nom du terminal

<form name="form5" method="post" action="">

<input type="text" name="nomterminal" value="&lt;?php echo htmlspecialchars($nomterminal); ?&gt;" />

</form>

Adresse IP


<input type="text" name="adrip" value="&lt;?php echo htmlspecialchars($adrip); ?&gt;" />


<input type="submit" value="Envoyer" />

```

6 réponses

R4f Posted messages 441 Status Membre 62
 
Well, I made you the complete solution:

Data (file donnees.txt):
 terminal3,192.368.0.3 terminal1,192.168.0.1 terminal2,192.268.0.2 terminal4,192.468.0.4 


Main entry point of the program (liste_terminaux.php):

 <?php $nom_fichier = 'donnees.txt'; $terminaux = file($nom_fichier); foreach ($terminaux as $index => $donnee) { list($nom, $adresse) = explode(',', $donnee); $infos_terminal[$index]['nom'] = $nom; $infos_terminal[$index]['adresse'] = $adresse; } ?> <html><head><title>Available Terminals</title></head><body> <h1>Available Terminals</h1> <ul> <?php foreach($infos_terminal as $index => $donnees) { echo "<li><a href=\"form_terminal.php?ligne=$index\">{$donnees['nom']} - {$donnees['adresse']} </a></li>"; } ?> </ul> </body> </html> 


Displaying a terminal form (form_terminal.php):

 <?php if (!is_numeric($_GET['ligne'])) { header('Location: liste_terminaux.php'); exit(); } $ligne = (int) $_GET['ligne']; $nom_fichier = 'donnees.txt'; $terminaux = file($nom_fichier); list($nom, $adresse) = explode(',', $terminaux[$ligne]); ?> <html><head><title>Editing Terminal <?=$ligne ?></title></head><body> <h1>Editing Terminal <?=$ligne ?></h1> <form action="modif_terminal.php" method="post"> <input type="hidden" name="ligne" value="<?=$ligne ?>"> Terminal Name: <input type="text" name="nom" value="<?=$nom ?>"><br/>IP Address: <input type="text" name="adresse" value="<?=$adresse ?>"><br/> <input type="submit" value="Modify"> </form> </body> </html> 


Editing a terminal (modif_terminal.php):

 <?php $ligne = (int) $_POST['ligne']; $nom = $_POST['nom']; $adresse = $_POST['adresse']; $nom_fichier = 'donnees.txt'; $terminaux = file($nom_fichier); $terminaux[$ligne] = "$nom,$adresse\n"; file_put_contents($nom_fichier, implode("",$terminaux)); header('Location: liste_terminaux.php'); exit(); ?> 


It works for me.

I hope it works for you too and does what you want!

Raph
1
ced29
 
Hi, thank you very much for your help. Sorry I was absent for 2 days. I will test your code and keep you posted.
See you!
0
R4f Posted messages 441 Status Membre 62
 
Hi,

Actually, you're in a very classic situation where you would be better off putting your data into a relational database. You could possibly do this with a SQLite database.

Well, to answer your question, generally we don't work with text files because we face the same issue as you. Naturally, with a text editor, we think we can easily insert data into a text file, but in fact, the following characters also need to be shifted. In case of text destruction, the following characters must also be shifted to the left... It's a hassle!

So what we do if the data file isn't too large:
1- we read the original file (AAAA.txt) (e.g., with file() http://fr3.php.net/manual/fr/function.file.php)
2- we destroy it (or rather rename it, you never know..., for example to AAAA.old)
3- in the data we have in memory, we make the appropriate modifications
4- we write the data into AAAA.txt (new file) for example with file_put_contents (http://fr3.php.net/manual/fr/function.file-put-contents.php)

Raph
0
ced29
 

<input type="text" value="&lt;?php
$file = fopen(" chemin="" fgets="" fclose="" fopen="" fseek="" substr="" echo="" rtrim="" />" />
0
ced29
 
Hi, I tested what you gave me, it works almost except when I try to modify some data it gives me: Fatal error: Call to undefined function: file_put_contents() ... on line 7
Apparently, it comes from the fact that I don't have PHP 5???
How can I modify the code so that it works?

Thanks in advance
@+
0
R4f Posted messages 441 Status Membre 62
 
hi,

yes, file_put_contents is specific to PHP5... but you just need to define it in PHP4:

 function file_put_contents($filename, $data) { if( $file = fopen($filename, 'w') ) { $bytes = fwrite($file, is_array($data) ? implode('', $data) : $data); fclose($file); return $bytes; // return the number of bytes written to the file } } 


Raph
0
brillante
 
I am creating a website. I have created several forms. The problem is that the data displayed in a form initially exists in an Excel file. How can I retrieve the data from an Excel file into the form I need using PHP or JavaScript?
0
R4f Posted messages 441 Status Membre 62
 
Hi,


There is a PHP project that seems to be working well for handling Excel (XLS) file content:

https://sourceforge.net/projects/phpexcelreader/


Raph
0