PHP:用于编辑文件的表单

PHP:用于编辑文件的表单,php,forms,edit,Php,Forms,Edit,我用PHP创建了一个小文件管理器。 阅读、删除、移动、编辑一切正常。 但在显示要修改的文件内容时,存在一个问题 在上一个表单中,我让用户选择要修改的文件,在另一个页面中,我获得文件名,并在文本区域中显示其内容,代码如下: <?php if (isset($_POST['file'])) { $file = $_POST['file']; ?> <form action="edit_file_process3.php" method="get"> <

我用PHP创建了一个小文件管理器。 阅读、删除、移动、编辑一切正常。 但在显示要修改的文件内容时,存在一个问题

在上一个表单中,我让用户选择要修改的文件,在另一个页面中,我获得文件名,并在文本区域中显示其内容,代码如下:

<?php
if (isset($_POST['file'])) {
    $file = $_POST['file'];
?>

<form action="edit_file_process3.php" method="get">
    <p>This is the content of the file <?php echo $file; ?>. Now you can modify it:</p>
<textarea rows="10" cols="70" name="content"><?php readfile($file)   ?></textarea>

<p><input type="submit" name="submit" value="edit <?php echo $file; ?>" /> </p>
</form>

<a href="read2.php">Come back to the file manager</a>

这是文件的内容。现在您可以修改它:


您必须确保脚本正在正确的目录中查找文件

您可以使用绝对路径,例如
/from/root/to/my/dir
,也可以使用相对路径,例如
。/where/is/my/file

如果使用的是相对路径,则必须具有与查找文件的脚本相关的正确路径。否则,您将无法打开流:没有像本例中那样的文件或目录

编辑


如果我理解正确,您正在尝试编辑文件系统中应该已经存在的文件。当你第一次尝试打开它时,你会得到你描述的这个错误。这是因为脚本正在错误的目录中查找文件。当你“编辑它”并保存它时,就像Barmar指出的那样,你很可能实际上在另一个目录中创建了一个新文件。在此之后,可能会有两个不同的文件存在于同名的不同目录中。另一个是显示在“文件资源管理器”中的文件,另一个是您实际编辑的文件。

您应该使用file\u get\u contents()和file\u put\u contents()函数,而不是readfile(),因为readfile()读取文件并将其直接写入输出缓冲区,如echo('file contents')是的。错误表明您试图读取的文件
prova4.txt
的路径不正确。我已尝试。使用file_get_contents()和echo file_get_contents(),但我得到了相同的结果error@budwiser但是如果我用其他东西修改了文本,它就会显示出正确的内容。如果是,则意味着(至少在第一次之后)文件被读取properly@johnnyfittizio,您所说的“用其他内容修改该文本”是什么意思?谢谢!你向我澄清情况!我看到前面的三个文件被保存到根文件夹中,其中包含内容修改。第四个文件(我现在要编辑的文件)不在那里,因为我还没有编辑它。因此,我将尝试修复路径。谢谢,我现在修复它。你是对的,文件并没有像我想的那样被编辑,而是重新创建并保存在根文件夹中。在“上传”文件夹中,它保留了文件内容,没有修改。因此,为了正确读取文件,我修改了文本区域中的这一行:以及编辑文件的php脚本中的这一行:$to_modify=fopen(“upload/”$filename,“w+”)或die(“在写入模式下打开文件时出错!”);现在一切都好了!我很高兴能帮上忙。快乐编码!:D
<br />
<font size='1'><table class='xdebug-error xe-warning xe-scream' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> SCREAM: Error suppression ignored for</th></tr>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: readfile(prova4.txt): failed to open stream: No such file or directory in C:\wamp\www\LEARNING\Files\edit_file_process3.php on line <i>50</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0004</td><td bgcolor='#eeeeec' align='right'>144848</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\LEARNING\Files\edit_file_process3.php' bgcolor='#eeeeec'>..\edit_file_process3.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0004</td><td bgcolor='#eeeeec' align='right'>145248</td><td bgcolor='#eeeeec'><a href='http://www.php.net/readfile' target='_new'>readfile</a>
(  )</td><td title='C:\wamp\www\LEARNING\Files\edit_file_process3.php' bgcolor='#eeeeec'>..\edit_file_process3.php<b>:</b>50</td></tr>
</table></font>