好的,这就是我正在做的事情。
我列出了目录的竞争,以便我可以编辑和删除文件。
我用来执行此操作的代码如下(其他则编辑,因为它不起作用)。
<?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']; ?>
.
关于php - 使用 PHP 编辑文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9320486/