1

我有一个脚本,它列出了我在一个目录中的所有 txt 文件。我已经为出现的每个结果都添加了一个超链接,我想重定向到一个我有代码的简单编辑器。

这是列出目录中文件的代码;

<?php
//Open directory
$dir = dir("content");
//List files in directory
while (($file = $dir->read()) !== false){
    //Make sure it's a .txt file
    if(strlen($file) < 5 || substr($file, -4) != '.txt')
        continue;

    echo "Page: <a href='content/" .$file. "'>" . $file . "</a><br />";
}

$dir->close();
?>

以及编辑文件的代码:

<?
if($_POST['Submit']){
    $open = fopen("content/body.txt","w+");
    $text = $_POST['update'];
    fwrite($open, $text);
    fclose($open);
    echo "File updated.<br />"; 
    echo "File:<br />";
    $file = file("content/body.txt");
    foreach($file as $text) {
        echo $text."<br />";
    }
}else{
    $file = file("content/body.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\" />\n
    </form>";
}
?>

尝试这样做的最佳方法是什么?

提前致谢。

4

1 回答 1

1

我想你的意思是:

在目录列表中使用这个:

echo "Page: <a href='editor_script.php?filename=".$file."'>".$file."</a><br/>";

在你的编辑器脚本中使用这个:

$open = fopen("content/".$_GET['filename'].".txt","w+");

$file = file("content/".$_GET['filename'].".txt");

我不确定,但也许你必须actionform

echo '<form method="post">';

或使用 url$_GET['filename']

echo '<form action="editor_script.php?filename='.$_GET['filename'].'" method="post">';

也许这也有效,但我不确定

echo '<form action="?filename='.$_GET['filename'].'" method="post">';
于 2013-06-02T23:36:54.407 回答