PHP文本编辑器

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>

Okay so index.php lists all text files in a directory as links and edit.php edits text files. The $file variable in edit.php is the path to the text file, how would I go about making the path the same as the text from the link once it is clicked? The idea being that once the text files link is clicked, it will be opened in the editor. Any help would be appreciated, thank you.

Not really sure if this is what you were trying to do... but I would add the filename to the URL with a ?file=$file which will pass the data in the URL as a GET entity that can be called from the edit.php file as edited below.

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>