Log in

View Full Version : Re-Open and Edit .txt File with PHP



coly
05-15-2014, 11:11 AM
hi,

I've build a PHP form that creates .txt files with a certain syntax, as you can see here:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="de">
<head>
<title></title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/charCount.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#message1").charCount({
allowed: 26,
warning: 5,
counterText: 'Verfügbare Zeichen: '
});
$("#message2").charCount({
allowed: 26,
warning: 5,
counterText: 'Verfügbare Zeichen: '
});
$("#message3").charCount({
allowed: 26,
warning: 5,
counterText: 'Verfügbare Zeichen: '
});
$("#message4").charCount({
allowed: 26,
warning: 5,
counterText: 'Verfügbare Zeichen: '
});
$("#message5").charCount({
allowed: 26,
warning: 5,
counterText: 'Verfügbare Zeichen: '
});
$("#message6").charCount({
allowed: 26,
warning: 5,
counterText: 'Verfügbare Zeichen: '
});
$("#message7").charCount({
allowed: 26,
warning: 5,
counterText: 'Verfügbare Zeichen: '
});
});
</script>
<link href="css/style.css" rel="stylesheet">
</head>

<body>


<!-- TERMINE ANLEGEN FORMULAR -->

<div class="wrapper">
<div id="main" style="padding:50px 0 0 0;">
<form name="formular" id="formular" method="POST">

<h1>Termin anlegen</h1>


<!-- DATEINAME -->

<div>
<label>
<h3>Dateiname:</h3>
<input type="text" name="title" class="zeile">
</label>
</div>


<!-- ZEILE1 -->

<div>
<label>
<h3>Zeile1:</h3>
<input type="text" name="zeile1" class="zeile" id="message1" maxlength="26">
</label>
</div>

<!-- ZEILE2 -->

<div>
<label>
<h3>Zeile2:</h3>
<input type="text" name="zeile2" class="zeile" id="message2" maxlength="26">
</label>
</div>

<!-- ZEILE3 -->

<div>
<label>
<h3>Zeile3:</h3>
<input type="text" name="zeile3" class="zeile" id="message3" maxlength="26">
</label>
</div>

<!-- ZEILE4 -->

<div>
<label>
<h3>Zeile4:</h3>
<input type="text" name="zeile4" class="zeile" id="message4" maxlength="26">
</label>
</div>

<!-- ZEILE5 -->

<div>
<label>
<h3>Zeile5:</h3>
<input type="text" name="zeile5" class="zeile" id="message5" maxlength="26">
</label>
</div>

<!-- ZEILE6 -->

<div>
<label>
<h3>Zeile6:</h3>
<input type="text" name="zeile6" class="zeile" id="message6" maxlength="26">
</label>
</div>

<!-- ZEILE7 -->

<div>
<label>
<h3>Zeile7:</h3>
<input type="text" name="zeile7" class="zeile" id="message7" maxlength="26">
</label>
</div>

<br>

<!-- SPEICHERN -->

<div>
<input type="submit" value="Speichern" onclick="alert('Termin wird angelegt, bitte warten bis die Seite neu geladen wurde.');">
</div>

</form>


</div>
</div>
</body>
</html>




In the next step, I listed all .txt files so that they can be deleted again, here:


<html lang="de">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title></title>
<link href="css/style.css" rel="stylesheet">
</head>

<body>

<div class="wrapper">
<div id="main" style="padding:50px 0 0 0;">
<!-- TERMINE LÖSCHEN FORMULAR -->
<form id="formular" method="POST">
<h1>Termine löschen</h1>
<h2>Es wurde(n) <?php echo --$i-1 ?> Termin(e) gefunden. Die Datei "_gsdata_" dient zur Überprüfung und kann daher nicht gelöscht werden.</h2>
<table>
<thead>
<tr>
<th>Datei</th><th>löschen</th>
</tr>
</thead>

<tbody>
<?php echo $tablerows ?>
</tbody>
</table><br>
<input type="submit" value="Löschen" onclick="return confirm('Die ausgewählten Termine werden gelöscht! Sind Sie sicher?');">





</form>

</div>
</div>
</body>
</html>
What I am trying to do now, is making it possible to edit those .txt files after they are created. So that I next to my delete-checkbox, I have an edit-button, that opens the .txt file within the PHP form again, and if saved, it should overwrite the existing .txt file. Problem is, I don't have a clue how to do that.

jscheuer1
05-15-2014, 03:54 PM
With the advent of PHP 5 one can use file_put_contents with the append flag, The exclusive flag is a good idea too if more than one person might be trying to write to the file file at the same time. Example 2 on the manual page:


<?php
$file = 'people.txt';
// The new person to add to the file
$person = "John Smith\n";
// Write the contents to the file,
// using the FILE_APPEND flag to append the content to the end of the file
// and the LOCK_EX flag to prevent anyone else writing to the file at the same time
file_put_contents($file, $person, FILE_APPEND | LOCK_EX);
?>

is a perfect one for what you're asking. Read more at:

http://us2.php.net/manual/en/function.file-put-contents.php


Notes: You may want to use javascript and even jQuery to post via AJAX to a page that will do the actual writing because otherwise you would have to submit a form (including reloading of the page or loading of another page) each time you wanted to edit a file. Also because with the LOCK_EX flag there's always a possibility the file will not be written to. If that happens you want feedback so that the write operation can be retried. But before I get into any of that, the above code is what you've asked for, see if you can get it working to your satisfaction.