簡體   English   中英

使用 PHP fwrite 從下拉表單中選擇的文件寫回文本文件時出現問題

[英]Trouble Writing back to text file using PHP fwrite from file selected from a dropdown form

我在寫回從帶有下拉菜單的 html 表單中選擇的文本文件時遇到問題,腳本能夠讀取但無法寫回,返回“無法打開文件”。使用 PHP fopen 和 fwrite

這是 index.html 代碼:

<html>
<head></head>
<title>Edit Server Side Text Files</title>
<body>
<form action="function.php" method="post">
<select name="list">
  <option value="./files/banned-kdf.txt">banned-kdf.txt</option>
  <option value="./files/blackList.txt">blackList.txt</option>
</select>
<input type="submit" name="edit" value="Edit" />
</form>
</body>
</html>

這是 function.php 代碼:

<?php

if(isset($_POST["edit"])) {
   $filename = $_POST["list"];
   global $filename;
//   var_dump($filename);
   $myfile = fopen("$filename", "r") or die("Unable to open file!");
   $filecontent =  fread($myfile,filesize("$filename"));
   fclose($myfile);
 }
?>
<html>
<form action="<?php echo $PHP_SELF;?>" method="post">
<textarea name="textareaContent" rows="50" cols="100">
<?

if(isset($filecontent)) { echo $filecontent; }

?>
</textarea>
<br>
<br>
<input type="submit" name="update" value="Update" />
 </textarea>
<?
//write to text file
 if(isset($_POST["update"])) {
// The following two lines are just for troubleshooting and have been commented out.     
//   echo $_POST["update"];
//   var_dump($filename);
   $myfile = fopen("$filename", "w") or die("Unable to open file!");
   fwrite($myfile, $_POST["textareaContent"]);
   fclose($myfile);
 }
 ?>

 </form>
 <br>
 <br>
<a href="/index.html">Return to List Selection</a>
</html>

請檢查目標文件的權限以及該文件所在的文件夾。

您不是以第二種形式發送文件名,只是代碼,因此, $filename未定義。 將其添加到隱藏的輸入中:

global $variable; 在這里沒有用,因為在處理表單時該變量將不會出現在下一個腳本執行中。

<?php

// Default value for contents:
$filecontent = '';

// Ok, read the file here
if(isset($_POST["edit"])) {
   $filename = $_POST["list"];

   global $filename; // This will not be useful

   $myfile = fopen("$filename", "r") or die("Unable to open file!");
   $filecontent =  fread($myfile,filesize("$filename"));
   fclose($myfile);
 }
// If request comes from second form, process it
if(isset($_POST['update'])) {
   // Get file name from input hidden
   $filename = $_POST["filename"];

   $myfile = fopen("$filename", "w") or die("Unable to open file!");
   fwrite($myfile, $_POST["textareaContent"]);
   fclose($myfile);
   // End the script, no need to show the form again
   die('File updated successfully.');
}
?>
<html>
<form action="<?php echo $PHP_SELF;?>" method="post">
<input type="hidden" name="filename" value ="<?php
    // Send filename within form
    echo $filename;
?>">
<textarea name="textareaContent" rows="50" cols="100">
<?
// No if needed here, we already have a value
echo $filecontent;
?>
</textarea>
<br>
<br>
<input type="submit" name="update" value="Update" />

 </form>
 <br>
 <br>
<a href="/index.html">Return to List Selection</a>
</html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM