I created a PHP form that creates a .txt
file with a certain syntax. For example: When I enter mouse in text field number one and house in text field number to, it creates a .txt file with the following content:"data1="mouse",data2="house"
.
So far so good. Now I would like to create another PHP site that shows every .txt file in a certain webfolder, displays each of those files with a checkbox to delete it. And here is my problem I don't know how to do that:
I want that each of the displayed file names is a hyperlink. If a user clicks on one of these links, it should read the .txt and take those elements I entered in my first PHP (mouse, house) and put it back into a form. So that I can edit and update existing .txt files.
Here is how far I have come, unfortunately I'm a lousy programmer. Has somebody done something like this before and could share the code?
<?php
include('auth.php');
$ordner = "txt/*";
$tablerows = "";
$i = 1;
foreach (glob("$ordner") as $filename) {
if (isset($_POST['checkbox'][$i]))
unlink(realpath($filename));
$i++;
} $i = 1;
foreach (glob("$ordner") as $filename) {
$tablerows .= "<tr><td>" . basename($filename) . "</td><td><input type='checkbox' name='checkbox[$i]'></td></tr>
";
$i++;
}
?>
<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. </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
Try this piece of code instead of yours:
<?php
$path = "txt/*.txt";
// delete files
if (isset($_POST['delete']) && is_array($_POST['delete'])) {
foreach ($_POST['delete'] as $filename) {
unlink(realpath($filename));
}
}
// prepare HTML table rows
$tablerows = "";
$i = 0;
foreach (glob($path) as $filename) {
$tablerows .= "<tr>".
"<td>" . basename($filename) . "</td>".
"<td><input type='checkbox' name='delete[]' value='$filename'></td>".
"</tr>
";
$i++;
}
?>
Thanks for the answers. What I am trying to do is to open a .txt file. Put specific parts of it into a form and if the form is submited, the .txt file should be updated. So something like the code below. My problem is, my text file has a specific format:
data1="first text",data2="second text",data3="third text"
I only want to read an exchange the parts between the ""! So if opened in a form, the first text input should have the word first text in it, the second one the word second text, and so on ... So when I change the content in the first text input to test and submit the form, my .txt file should then have the following content:
data1="test",data2="second text",data3="third text"
<?
if($_POST['Submit']){
$open = fopen("test.txt","w+");
$text = $_POST['update'];
fwrite($open, $text);
fclose($open);
echo "File updated.<br />";
echo "File:<br />";
$file = file("test.txt");
foreach($file as $text) {
echo $text."<br />";
}
echo "<p><a href=\"./livepage.php\">click here to view the live updated webpage</a></p>";
echo "<p><a href=\"./mainadminpage.php\">click here to view the admin menu</a></p>";
}else{
$file = file("test.txt");
echo "<form action=\"".$PHP_SELF."\" method=\"post\">";
echo "<textarea Name=\"update\" cols=\"50\" rows=\"10\">";
foreach($file as $text) {
echo $text;
}
echo "</textarea>";
echo "<input name=\"Submit\" type=\"submit\" value=\"Update\" />
</form>";
}
?>