Auf das Thema antworten  [ 2 Beiträge ] 
Tutorial: Writing and reading files for PHP with include 
Autor Nachricht
Administrator
Benutzeravatar

Registriert: Sa 15. Dez 2012, 19:15
Beiträge: 137
Wohnort: Karlsruhe
Mit Zitat antworten
I was looking for a efficent way to store and load data for my commentary-system. After some tries I got the idea to store the data in a file in a way, so that I can include it afterwards for loading and this is an example for this procedure.
Code:
 
<?
 
 // Check for the file
 $filePath = "example.txt";
 $file = "";
 if(!file_exists($filePath)) { $file = fopen($filePath, "w+"); fwrite($file, "<?php \n"); } // If file does not exists, create it with the first line
 else { $file = fopen($filePath, "a+"); } // else open it for appending data
 
 if(isset($_GET['var1']) AND isset($_GET['var2'])) // If true, the user wants to add informations
 {  
    $success = fwrite($file, "\$var1[] ='" . $_GET['var1'] . "'; " . "\$var2[]='" . $_GET['var2'] . "';\n");
    if($success)
    {
        echo "<p>These data has been stored: var1 = " . $_GET['var1'] . " AND var2 = " . $_GET['var2'] . " @ " . $filePath . "</p>";
    }
    else
    {
        echo "<p>An error has occurred!</p>";
    }
 }
 else // The user wants the output
 {  
    $var1 = array();
    $var2 = array();    
 
    rewind($file); /* Set the file position to the beginning of the stream */
    include($filePath);    
 
    $count = count($var1);
    for($index = 0; $index < $count, $index++) {
    echo "<p> Entry " . $index . ": var1 = " . $var1[$index] . "; var2 = " . $var2[$index] . ";</p>";
    }
 
  unset($count);
  unset($var2);
  unset($var1);
 }
 
 fclose($file);
 unset($file);
 unset($filePath);
 
?>
 

You can also extend it for three or more variables. There is no really more to say as already standing in the comments. If you want to use more than this two variables, make sure, you initialize them before using include. If you want to modify or to change the number of variables, you may have to overwrite the whole file if you don't have a suitable wrapper.


Mo 17. Dez 2012, 22:45
Diesen Beitrag melden
Profil Website besuchen
Administrator
Benutzeravatar

Registriert: Sa 15. Dez 2012, 19:15
Beiträge: 137
Wohnort: Karlsruhe
Mit Zitat antworten
There is another option to store named variables:
Code:
 
<?
 
 // Check for the file
 $filePath = "example.txt";
 $file = "";
 if(!file_exists($filePath)) { $file = fopen($filePath, "w+"); fwrite($file, "<?php\n"); rewind($file); } // If file does not exists, create it with the first line
 else { $file = fopen($filePath, "r+"); } // else open it for appending data
 
 if(isset($_GET['var1']) AND isset($_GET['var2'])) // If true, the user wants to add informations
 {
    $success = fwrite($file, "\<?php $db['var1'] = '" . $_GET['var1'] . "'; " . "\$db['var2'] = '" . $_GET['var2'] . "';\n");
    if($success)
    {
        echo "<p>This data has been stored: var1 = " . $_GET['var1'] . " AND var2 = " . $_GET['var2'] . " @ " . $filePath . "</p>";
    }
    else
    {
        echo "<p>An error has occurred!</p>";
    }
 }
 
 
 
 else // The user wants the output
 {
    $db = array();
    $db['var1'] = "";
    $db['var2'] = "";
 
    include($filePath);    
 
   echo "<p> Values: var1 = " . $db['var1'] . "; var2 = " . $db['var2'] . ";</p>";
 
  unset($db);
 }
 
 fclose($file);
 unset($file);
 unset($filePath);
 
?>
 



Best regards,
Magony

_________________
Bei Fragen, Lob, Kritik, Vorschläge, hilfreiche Hinweise oder Alternativvorschläge: Beitrag, neues Thema oder PN.
Für Dinge die diskutiert werden sollten, bitte neues Thema im jeweiligen Forum.
Wenn du nicht weißt wohin: Forum Unsortiert.


Mo 17. Dez 2012, 22:49
Diesen Beitrag melden
Profil Website besuchen
Beiträge der letzten Zeit anzeigen:  Sortiere nach  
Auf das Thema antworten   [ 2 Beiträge ] 

Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder und 1 Gast


Du darfst neue Themen in diesem Forum erstellen.
Du darfst Antworten zu Themen in diesem Forum erstellen.
Du darfst deine Beiträge in diesem Forum nicht ändern.
Du darfst deine Beiträge in diesem Forum nicht löschen.
Du darfst keine Dateianhänge in diesem Forum erstellen.

Suche nach:
Gehe zu:  
cron
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software
Deutsche Übersetzung durch phpBB.de

Impressum