使用PHP编辑文件


Edit a file with PHP

好的,这是我要做的。

我列出了一个目录的比赛,这样我就可以编辑和删除文件。

我用来做这件事的代码如下(其他然后编辑,因为它不起作用)。

<?php
$directory = ("enctlfiles/");
$dir = opendir($directory);
$files = array();
while (($file = readdir($dir)) != false) {
  $files[] = $file;
}
closedir($dir);
sort($files);
print("<TABLE border=1 cellpadding=5 cellspacing=0 class=whitelinks>'n");
print("<TR><TH>File Name</TH><th>Modified Date</th><th>Entries In File</th><th>Delete | Edit</th></TR>'n");
foreach ($files as $file)
{
    if (strpos($file, '.jpg',1)||strpos($file, '.ctl',1) ){
      echo "<TR><TD><a href=$directory$file>$file</a></td>";
      echo "<td>";
      echo date("F d Y H:i:s",filemtime($directory. $file));
      echo "<td>";
      echo count(file($directory. $file));
      echo "<td>";
      echo "<a href=enctlfiles/dodelete.php?file=$file>Delete</a> | <a href=enctlfiles/editfile.php?file=$file>Edit</a>";
    }
}
?>

它将所有文件按顺序排列在一个漂亮的表中。删除功能有效。编辑版没那么多。这是由于我不知道自己在做什么,我确信这一点

以下是editfile.php 的内容

<form action="edit.php" method="post">
  <TEXTAREA NAME="save" COLS=150 ROWS=50 wrap="off">
  <?php
  $dir = ".";
  include($dir. '/' .$_GET['file']);
  ?>
  </textarea>
  <P><INPUT TYPE="SUBMIT" VALUE="Update File"><INPUT TYPE="RESET">
</FORM>

当您单击"编辑"链接时,它会用文件内容填充文本区域。我不知道这是否是获取数据以编辑文件的正确方法。

以下是edit.php文件的内容:

<?php
//I have tried this but it give me that Unable to open file.
//$org = ($_GET['file']) or exit("Unable to open file!"); 
//This returns me to the prodsh.php page but does not edit the file.
$org = $_POST['save'] or exit("Unable to open file!"); 
$copy = $org . date("-m-d-Y-His");
copy($org,$copy);
$f = fopen($org, 'w');
fwrite($f, $_POST['save']);
fclose($f);
header('Location: http://somesite.com/GroveTuckey/itemsetup/prodsh.php');
?>

我认为editfile.php和edit.php文件被顶起了,但不知道在哪里。

我正在努力学习这一点,当我遇到困难时,这个网站非常有帮助,所以我提前感谢你的帮助。

我也知道通过网页编辑文件的危险。这不是公共的,也不是在全世界都可以访问的服务器上。现在我不想处理数据库。

我使用以下代码编辑了文件:

<form action="doedit.php" method="post">
  <TEXTAREA NAME="save" COLS=150 ROWS=50 wrap="off">
  <?php
  include('somefile.ctl');
  ?>
  </textarea>
  <P><INPUT TYPE="SUBMIT" VALUE="Update File"><INPUT TYPE="RESET">
</FORM> 

doedit.php的内容为:

<?php
$org = 'Somefile.ctl';// This is the name of the file in the form above
$copy = $org . date("-m-d-Y-His");
copy($org,$copy);
$f = fopen('somefile.ctl', 'w');//This is the name of said file also.
fwrite($f, $_POST['save']);
fclose($f);
header('Location: http://somesite.com/GroveTickey/editfile.php');
?>

但由于文件的名称可以是任何东西,我不能在脚本中放入特定的名称。

除了必须对文件名进行硬编码之外,这种方法效果很好。

您没有将文件名传递回edit.php。将表单的操作设置为edit.php?file=<?php echo $_GET['file']; ?>

El codigo para editar lo hice asi:
---Inicio
<? php
$filename = isset($_GET['file']) ? $_GET['file'] : '';
$directory = ("C:''Linux''www''contenido''"); // Restrict to this directory...
$fn = $directory . $filename;
if (isset($_POST['content']))
{
    $content = stripslashes($_POST['content']);
    $fp = fopen($fn,"w") or die ("Error opening file in write mode!");
    fputs($fp,$content);
    fclose($fp) or die ("Error closing file!");
}
?>
<form action="<?php echo $_SERVER["PHP_SELF"] ?>" method="post">
    <textarea rows="25" cols="100" name="content"><?php readfile($fn); ?></textarea>
    <hr />
    <input type="submit" value="Guardar">
</form>
<a href="####">back to list</a>
And work great