title: how to can i renameFile in php
body: <p>i have a Problem in this code.
  i want add a column for rename file to my program but i can't add this code exactly.on real i want that i can edit name for evry files.
  pleas help me for write function renameFile in code.</p>
  
  <p>my code : </p>
  
  <pre><code>&lt;?php
  if (isset( $_POST["saveFile"] ) ) {
      saveFile();
  } elseif (isset( $_POST["createFile"] ) ) {
      createFile();
  } elseif (isset($_GET["filename"] ) ) {
      displayEditForm();
  } elseif(isset($_POST["subrename"])){
      renameFile();
  } else {
      displayFileList();
  }
  
  function displayFileList() {
      displayPageHeader();
      ?&gt;
      &lt;h2&gt;Choose a file to edit:&lt;/h2&gt;
      &lt;table cellspacing="0" border="0" style="width: 750px; border: 1px solid #666666;"&gt;
          &lt;tr&gt;
              &lt;th&gt;Filename&lt;/th&gt;
              &lt;th&gt;RenameFile&lt;/th&gt;
              &lt;th&gt;Size (bytes)&lt;/th&gt;
              &lt;th&gt;Last Modified&lt;/th&gt;
          &lt;/tr&gt;
          &lt;?php
          $dir = opendir(".");
          while ( $filename = readdir($dir) ) {
              if (substr($filename,strlen($filename)-4,4) == ".txt"){
                  echo '&lt;tr&gt;&lt;td&gt;&lt;a href="?filename='.$filename.'"&gt;'.$filename.'&lt;/a&gt;&lt;/td&gt;';
                  echo '&lt;td&gt;&lt;a href="?rename='.$filename.'"&gt;'.$filename.'&lt;/a&gt;&lt;/td&gt;';
                  echo '&lt;td&gt;'.filesize($filename) . '&lt;/td&gt;';
                  echo '&lt;td&gt;'.date("M j, Y H:i:s", filemtime($filename))."&lt;/td&gt;&lt;/tr&gt;\n\t\t";
              }
          }
          closedir($dir);
          ?&gt;
      &lt;/table&gt;
      &lt;h2&gt;Or create a new file:&lt;/h2&gt;
      &lt;form action="example6.php" method="post"&gt;
          &lt;div style="width: 700px;"&gt;
              &lt;label for="filename"&gt;Filename&lt;/label&gt;
              &lt;div style="float: right; width: 38%;"&gt; .txt&lt;/div&gt;
              &lt;input type="text" name="filename" style="width: 50%;" /&gt;
              &lt;div style="clear: both;margin: 5px;"&gt;
                  &lt;input type="submit" name="createFile" value="Create File" /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  &lt;?php
  }
  
  function displayEditForm() {
      if(isset($_POST["filename"]))
          $filename = $_POST["filename"].".txt";
      else
          $filename = $_GET["filename"];
      if (!file_exists( $filename ))
          die( "File not found" );
      displayPageHeader();
      ?&gt;
      &lt;h2&gt;Editing &lt;?php echo $filename ?&gt;&lt;/h2&gt;
      &lt;form action="example6.php" method="post"&gt;
          &lt;div style="width: 500px;"&gt;
              &lt;input type="hidden" name="filename" value="&lt;?php echo $filename; ?&gt;" /&gt;
              &lt;textarea name="fileContents" rows="20" cols="80" style="width: 100%;"&gt;&lt;?php
                  echo file_get_contents($filename);
                  ?&gt;&lt;/textarea&gt;
              &lt;div style="clear: both;"&gt;
                  &lt;input type="submit" name="saveFile" value="Save File" /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  &lt;?php
  }
  ?&gt;
  
  &lt;?php
      function renameFile(){
              $filename = $_POST["filename"];
          if (!file_exists( $filename ))
              die( "File not found" );
          rename($filename,$_POST["reFile"]);
          displayPageHeader()
          ?&gt;
          &lt;h2&gt;renameFile&lt;?php echo $filename ?&gt;&lt;/h2&gt;
          &lt;form method="post" action="example6.php"&gt;
              &lt;div style="width:500px; "&gt;
                  &lt;div style="width: auto;"&gt;
                      &lt;input type="hidden" name="filename" value="&lt;?php echo $filename ?&gt;" /&gt;
                      &lt;input type="text" name="reFile"/&gt;
                      &lt;input type="submit" name="subrename" value="rename" /&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/form&gt;
  &lt;?php
      }
  ?&gt;
  
  &lt;?php
  function saveFile() {
      $filename = $_POST["filename"];
      file_put_contents($filename, $_POST["fileContents"] );
      displayFileList();
  }
  
  function createFile() {
      $filename = $_POST["filename"];
      $filename .= ".txt";
      if (file_exists($filename)) {
          displayFileList();
          echo '&lt;script&gt;alert("The file '.$filename.' already exist!")&lt;/script&gt;';
      } else {
          file_put_contents($filename, "");
          displayEditForm();
      }
  }
  
  function displayPageHeader() {
  ?&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;A simple text editor&lt;/title&gt;
      &lt;style type="text/css"&gt;
          th {
              text-align: left;
              background-color: #aaaaaa;
          }
          td,th{
              padding: 5px;
          }
      &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;h1&gt;A simple text editor&lt;/h1&gt;
  &lt;?php
  }
  ?&gt;
  </code></pre>