PHP:用于编辑文件的表单

时间:2013-06-20 16:16:54

标签: php forms edit

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

在上一个表单中,我留给用户选择要修改的文件,在另一个页面中我获取文件名,并在textarea中显示其内容与此代码:

<?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>

我第一次选择文件时,在textarea而不是其正确的内容中,它显示如下:

<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>

如果我用其他内容覆盖此文本,那么它可以正常工作,下一次,textarea将显示该文件的正确内容。 但第一次这样做是非常令人讨厌的。 为什么会这样?

1 个答案:

答案 0 :(得分:0)

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

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

如果您使用相对路径,则必须具有相对于查找文件的脚本的正确路径。否则你会被唠叨无法打开流:没有像这种情况那样的文件或目录

修改

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