首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP文本编辑器

PHP文本编辑器
EN

Stack Overflow用户
提问于 2014-02-08 21:17:11
回答 1查看 2.2K关注 0票数 0

index.php

代码语言:javascript
复制
        <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

代码语言:javascript
复制
<?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中是文本文件的路径,单击链接后如何使路径与链接中的文本相同?这个想法是,一旦点击了文本文件链接,它就会在编辑器中打开。任何帮助都将不胜感激,谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-08 21:24:18

不太确定这是不是你想做的..。但是我会使用?file=$file将文件名添加到URL中,这将把URL中的数据作为一个GET实体传递,该实体可以按照下面的编辑从edit.php文件中调用。

index.php

代码语言:javascript
复制
    <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

代码语言:javascript
复制
<?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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21651931

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档