2014-02-08 71 views
0

的index.phpPHP文本编辑器

 <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> 

好了,所以index.php文件列出的目录中的链接和edit.php编辑文本文件中的所有文本文件。 edit.php中的$ file变量是文本文件的路径,我将如何使路径与链接中的文本一旦被点击相同?这个想法是,一旦文本文件链接被点击,它将在编辑器中打开。任何帮助将不胜感激,谢谢。

+0

怎么样把它的查询字符串?将它作为GET参数发送将是最简单的方法。 ('echo“".$file."”;')。首先我会考虑这个过程中的安全问题。 – jtheman

+0

没有真正担心安全。该代码是为大学任务。我自己对PHP很陌生。谢谢您的帮助。 – ech0

回答

0

不确定这是否是您正在尝试做的......但是我会使用?file = $文件将文件名添加到URL中,该文件将URL中的数据作为GET实体传递如下编辑,从edit.php文件调用。

的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> 
+0

这正是我想要做的。谢谢。 – ech0

+0

欢迎您,很高兴我能帮到您! – Kai

+0

我想urlencode的文件名,以防万一'urlencode($文件)'然后urldecode它回到edit.php http://se1.php.net/urlencode – jtheman