dongtu0363 2014-02-08 21:17
浏览 39
已采纳

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.

  • 写回答

1条回答 默认 最新

  • dousi6701 2014-02-08 21:24
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 从Freecad中宏下载的DesignSPHysics,出现如下问题是什么原因导致的(语言-python)
  • ¥30 notepad++ 自定义代码补全提示
  • ¥15 MATLAB有限差分法解一维边值问题
  • ¥200 内网渗透测试 横向渗透 Windows漏洞 Windows权限维持
  • ¥15 数据结构图的相关代码实现
  • ¥15 python中aiohttp.client_exceptions.ContentTypeError
  • ¥30 DeepLung肺结节检测生成最大froc值对应的epoch报错
  • ¥15 信号发生器如何将频率调大,步尽值改成10
  • ¥15 keil 5 编程智能家具,风扇台灯开关,人体感应等
  • ¥100 找一名渗透方面的专家