繁体   English   中英

PHP:用于编辑文件的表单

[英]PHP: form for editing files

我已经用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>

第一次选择文件时,在文本区域而不是其适当的内容中,它看起来像这样:

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

如果我用其他东西覆盖了此文本,那么它将起作用,并且下次,文本区域将显示文件的正确内容。 但是第一次这样做很讨厌。 为什么会这样?

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

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

如果使用相对路径,则必须具有相对于寻找文件的脚本的正确路径。 否则,您将无法打开流而陷入困境:没有这种情况下的文件或目录

编辑

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM