0

我正在编写一个应用程序来删除和修改目录中的文件。我成功删除了文件。现在我想在另一个页面上使用 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 以进行修改。

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

4

1 回答 1

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><?
    }
}
于 2013-08-12T06:03:54.267 回答