php将文本文件内容发布到另一页上的textarea

时间:2013-08-12 05:59:36

标签: php

我正在编写一个应用程序来删除和修改目录中的文件。我成功删除了文件。现在我想在另一页上使用textarea修改和更新同一文件的内容。

<form enctype="multipart/form-data" method="post">
  <div class="span7">
  <table>
   <thead>
    <tr>
     <th> List of files </th>
    <tr>
   </thead>
    <?php
    $files = glob("UploadFile/"."*");
    foreach($files as $txt)
    {
     if(mime_content_type($txt)=="text/plain")
     {
      $txtname = basename($txt);
      echo "<tr><td><input type='radio' name='txt' value='$txtname'/>&emsp;".$txtname."</td></tr>";
     }
    }
    ?>
  </table>
  </div>
  <div class="span8">
   <button type="submit" name="submit" value="Edit">Edit</button>
   <button type="submit" name="submit" value="Delete">Delete</button>
  </div>
  <?php
  global $txtname;
  $val = $_POST["submit"];
  $txtname = $_POST["txt"];
   if($val=="Delete")
      unlink("UploadFile/".$txtname);
       else if($val=="Edit")
   {
    $content=file_get_contents("UploadFile/".$txtname);
    header('Location:/edit.php');
           /* send content to this 'edit.php' page */
     }
  ?>
  </form>

在edit.php页面,我只是检查代码是否正常工作。它工作正常。

 <?php
 if($_POST['append'])
{
 $file_open = fopen("UploadFile/file.txt","a+");
 fwrite($file_open, $_POST['append']);
 fclose($file_open);
}
?>  
  <form enctype="multipart/form-data" action="<?=$PHP_SELF?>" method="post">
  <textarea name="append" value="">
    <?php
    echo $content;
    $datalines = file ("UploadFile/file.txt");
    foreach ($datalines as $zz)
    {
     echo $zz;
    }
    ?>
 </textarea>
  <button type="submit" name="submit" value="save"> Save </button>

如何将文件内容发送到textarea进行修改。

更新:我想将更改保存到同一个文件中。谢谢

1 个答案:

答案 0 :(得分:1)

<?php
if(isset($_POST['text']))
{
    file_put_contents("file.txt",$_POST['text']);
    header("Location: ".$_SERVER['PHP_SELF']);
    exit;
}
$text = file_get_contents("file.txt");
?>  
<form method="post">
<textarea name="text"><?=$text?></textarea>
<input type="submit">
</form>

列表

if(isset($_POST['text']))
{
    file_put_contents("UploadFile/".basename($_POST['file']),$_POST['text']);
    header("Location: ".$_SERVER['PHP_SELF']);
    exit;
}
if(isset($_GET['file']))
{
    $text = file_get_contents("UploadFile/".basename($_GET['file']));
?>  
    <form method="post">
    <input type="hidden" name="file" value="<?=urlencode($_GET['file'])?>">
    <textarea name="text"><?=$text?></textarea>
    <input type="submit">
    </form>
<?
} else {

    $files = glob("UploadFile/*");
    foreach ($files as $f)
    {
        $f=basename($f);
?><a href="?file=<?=urlencode($f)?>"><?=htmlspecialchars($f)?></a><br><?
    }
}