PHP 阅读文件后取消链接
PHP unlink files after read them
我读取文件夹中的文件。
现在我想让每个文件都有一个 "edit"、"start" 和 "delete" 按钮。
问题是当我点击"delete"时,PHP会删除最后读取的文件,而不是我点击"delete"
的文件
我知道这是 While 循环,它不会为取消链接函数提供任何信息,但我没弄对。
感谢帮助! :)
<?php
if ($handle = opendir('rezepte/')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
#Entfernt der Datei das ".txt"
$replacer = str_replace(".txt","",$entry);
echo "
<div class='eintrag'>$replacer</div><br>
<a href='$replacer.txt'>edit</a>
<a href='$replacer'> start</a>
<form method='post'>
<input type='submit' name='delete' value='delete'/>
</form>";
}
}
closedir($handle);
}
if(isset($_POST['delete'])) {
echo "
<div class='warning'>";
echo $replacer;
echo "Do you really want to delete?>
<form method='post'>
<input type='submit' name='really_delete' value='delete it'/>
</form>
<form method='post'>
<input type='submit' name='not_delete' value='cancel'/>
</form></div>";
}
if(isset($_POST['really_delete'])) {
unlink("rezepte/".$replacer.".txt");
}
if(isset($_POST['not_delete'])) {
}
?>
忽略上面代码中的一些语法错误,您可以将文件名 ($replacer) 放入删除提交按钮的值中,如下所示:
<input type='submit' name='delete' value='" . $replacer . "'/>
这会将 $replacer 的值发送到您似乎需要它的地方。
不过,理想情况下,像这样放置一个带有名称的隐藏表单字段会更有意义:
'<input type="hidden" name="filename" value="' . $replacer . '"/>'
并更改您的表单处理器以查找 $_POST['filename']。
嗯,根据你的编码风格,你必须编写如下代码:
<?php
if(isset($_POST['delete'])) {
$replacer = $_POST['delete'];
echo "
<div class='warning'>";
echo $replacer;
echo "Do you really want to delete?>
<form method='post'>
<input type='submit' name='really_delete' value='$replacer'/>
</form>
<form method='post'>
<input type='submit' name='not_delete' value='cancel'/>
</form></div>";
}
if(isset($_POST['really_delete'])) {
$replacer = $_POST['really_delete'];
unlink("rezepte/".$replacer.".txt");
}
if(isset($_POST['not_delete'])) {
}
if ($handle = opendir('rezepte/')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
#Entfernt der Datei das ".txt"
$replacer = str_replace(".txt","",$entry);
echo "
<div class='eintrag'>$replacer</div><br>
<a href='$replacer.txt'>edit</a>
<a href='$replacer'> start</a>
<form method='post'>
<input type='submit' name='delete' value='$replacer'/>
</form>";
}
}
closedir($handle);
}
我读取文件夹中的文件。
现在我想让每个文件都有一个 "edit"、"start" 和 "delete" 按钮。
问题是当我点击"delete"时,PHP会删除最后读取的文件,而不是我点击"delete"
的文件我知道这是 While 循环,它不会为取消链接函数提供任何信息,但我没弄对。
感谢帮助! :)
<?php
if ($handle = opendir('rezepte/')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
#Entfernt der Datei das ".txt"
$replacer = str_replace(".txt","",$entry);
echo "
<div class='eintrag'>$replacer</div><br>
<a href='$replacer.txt'>edit</a>
<a href='$replacer'> start</a>
<form method='post'>
<input type='submit' name='delete' value='delete'/>
</form>";
}
}
closedir($handle);
}
if(isset($_POST['delete'])) {
echo "
<div class='warning'>";
echo $replacer;
echo "Do you really want to delete?>
<form method='post'>
<input type='submit' name='really_delete' value='delete it'/>
</form>
<form method='post'>
<input type='submit' name='not_delete' value='cancel'/>
</form></div>";
}
if(isset($_POST['really_delete'])) {
unlink("rezepte/".$replacer.".txt");
}
if(isset($_POST['not_delete'])) {
}
?>
忽略上面代码中的一些语法错误,您可以将文件名 ($replacer) 放入删除提交按钮的值中,如下所示:
<input type='submit' name='delete' value='" . $replacer . "'/>
这会将 $replacer 的值发送到您似乎需要它的地方。
不过,理想情况下,像这样放置一个带有名称的隐藏表单字段会更有意义:
'<input type="hidden" name="filename" value="' . $replacer . '"/>'
并更改您的表单处理器以查找 $_POST['filename']。
嗯,根据你的编码风格,你必须编写如下代码:
<?php
if(isset($_POST['delete'])) {
$replacer = $_POST['delete'];
echo "
<div class='warning'>";
echo $replacer;
echo "Do you really want to delete?>
<form method='post'>
<input type='submit' name='really_delete' value='$replacer'/>
</form>
<form method='post'>
<input type='submit' name='not_delete' value='cancel'/>
</form></div>";
}
if(isset($_POST['really_delete'])) {
$replacer = $_POST['really_delete'];
unlink("rezepte/".$replacer.".txt");
}
if(isset($_POST['not_delete'])) {
}
if ($handle = opendir('rezepte/')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
#Entfernt der Datei das ".txt"
$replacer = str_replace(".txt","",$entry);
echo "
<div class='eintrag'>$replacer</div><br>
<a href='$replacer.txt'>edit</a>
<a href='$replacer'> start</a>
<form method='post'>
<input type='submit' name='delete' value='$replacer'/>
</form>";
}
}
closedir($handle);
}