0

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.

2
  • What about putting it in the query string? Sending it as a GET parameter would be the easiest way. (echo "<a href=edit.php?file=".urlencode($file).">".$file."</a>";). First I would consider the security concerns in this procedure though.
    – jtheman
    Feb 8, 2014 at 21:23
  • No real worry about security. The code is for a college task. Am quite new to PHP myself. Thanks for the help.
    – ech0
    Feb 8, 2014 at 21:40

1 Answer 1

0

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>
3
  • That's exactly what I was trying to do. Thanks.
    – ech0
    Feb 8, 2014 at 21:39
  • I'd urlencode the filename just in case urlencode($file) then urldecode it back in edit.php se1.php.net/urlencode
    – jtheman
    Feb 8, 2014 at 21:42
  • Yeah you can do, this is best practice to ensure that no special characters end up in the URL and can't be parsed incorrectly, but if your reading from a list of filenames stripped from a directory of already uploaded files, they probably don't contain characters that would have issues within the URL. It won't hurt to encode it though so... its up to you I guess.
    – Kai
    Feb 8, 2014 at 21:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.