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