Jeg har fundet denne gode på en phpside..
Den skulle kunne skrive tekst til en txt fil, men hvordan får jeg den til at arbejde sammen med en formular?
Den skulle efter sigende også slette den gamle tekst i filen inden den indsætter den nye, er det korrekt?
<?php
$filename = 'test.txt';
$somecontent = "Add this to the file\\n";
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($somecontent) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
?> ¨
Der er en sektion til php spørgsmål ude til venstre
Når man skriver kode kan man lave kodeeksempel som starter med [ pre ] og slutter med [ / pre ] (uden mellemrum) Et eksempel kunne være:
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
Tekst: <textarea name="tekst"></textarea>
</form>
<?php
$filename = 'test.txt';
$somecontent = $_POST["tekst"];
if(strtoupper($_SERVER["REQUEST_METHOD"])!="POST")
exit();
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($somecontent) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
?>
Hilsen Mathias
[Redigeret d. 06/06-05 13:08:40 af Mathias Knudsen]