index.php
<h1>View text files in a directory</h1>
<form action="" method="POST">
Directory name: <input name="folderName4" type="text" required="required"> <br>
<input type="submit" value="View Directories Text Files">
</form>
<br>
<?php
if (isset($_POST['folderName4'])){
$foldername = $_POST["folderName4"];
$directory = "upload"."/".$foldername."/";;
$files = glob($directory . "*.txt");
foreach($files as $file)
{
echo "<a href=edit.php>".$file."</a>";
echo "<br>";
}
}
?>edit.php
<?php
$url = 'http://127.0.0.1/ccb/edit.php';
$file = ;
if (isset($_POST['text']))
{
file_put_contents($file, $_POST['text']);
// redirect to the form, avoids refresh warnings from the browser
header(sprintf('Location: %s', $url));
printf('<a href="%s">Moved</a>.', htmlspecialchars($url));
exit();
}
$text = file_get_contents($file);
?>
<form action="" method="post">
<textarea name="text"><?php echo htmlspecialchars($text) ?></textarea>
<input type="submit">
</form>好的,index.php将目录中的所有文本文件作为链接列出,edit.php编辑文本文件。$file变量在edit.php中是文本文件的路径,单击链接后如何使路径与链接中的文本相同?这个想法是,一旦点击了文本文件链接,它就会在编辑器中打开。任何帮助都将不胜感激,谢谢。
发布于 2014-02-08 21:24:18
不太确定这是不是你想做的..。但是我会使用?file=$file将文件名添加到URL中,这将把URL中的数据作为一个GET实体传递,该实体可以按照下面的编辑从edit.php文件中调用。
index.php
<form action="" method="POST">
Directory name: <input name="folderName4" type="text" required="required"> <br>
<input type="submit" value="View Directories Text Files">
</form>
<br>
<?php
if (isset($_POST['folderName4'])){
$foldername = $_POST["folderName4"];
$directory = "upload"."/".$foldername."/";;
$files = glob($directory . "*.txt");
foreach($files as $file)
{
echo "<a href='edit.php?file=".$file."'>".$file."</a>"; // <-- Filename part of URL
echo "<br>";
}
}
?>edit.php
<?php
$url = 'http://127.0.0.1/ccb/edit.php';
$file = $_GET['file']; // <-- Inserted GET here
if (isset($_POST['text']))
{
file_put_contents($file, $_POST['text']);
// redirect to the form, avoids refresh warnings from the browser
header(sprintf('Location: %s', $url));
printf('<a href="%s">Moved</a>.', htmlspecialchars($url));
exit();
}
$text = file_get_contents($file);
?>
<form action="" method="post">
<textarea name="text"><?php echo htmlspecialchars($text) ?></textarea>
<input type="submit">
</form>https://stackoverflow.com/questions/21651931
复制相似问题