使用 fopen($ file,'w') 将文本文件加载到表单中,但是该文件不可写,并且无法保存更改。显然是权限问题,但出于明显的安全原因,我不想将它们更改为777。有没有办法立即执行此操作,然后在保存后将其更改回原来的状态?
<?php $file = "$ServerRoot/text/test.txt";
// $ServerRoot is defined elsewhere and is to the root of the filesystem
// Retrieve the submitted changes and save to the file
if ($_SERVER['REQUEST_METHOD'] === "POST") :
$fh = fopen($file,'w') or die("Can't open file for writing.");
fwrite($fh,$_POST['textfield']);
fclose($fh);
echo "Content saved.";
endif;
// FOR DIAGNOSTIC PURPOSES ONLY
$TestFile = (file_exists($file)) ? "The file exists\n" : "The file does <font color=\"red\">NOT</font> exist\n";
$TestFile .= (is_readable($file)) ? "The file is readable\n" : "The file is <font color=\"red\">NOT</font> readable\n";
$TestFile .= (is_writable($file)) ? "The file is writable\n" : "The file is <font color=\"red\">NOT</font> writable\n";
$TestFile = nl2br($TestFile);
// Read the textfile
$text = file_get_contents($file);?>
<div align="center">
<?=$TestFile;?>
<form action="<?=$submitFormaction;?>" method="post">
<div><input type="submit" value="Save File" />
<input type="reset" value="Reset"/></div>
<div><textarea name="textfield" rows="25" cols="85"><?=htmlspecialchars($text);?></textarea></div>
<div><input type="submit" value="Save File" />
<input type="reset" value="Reset"/></div>
</form>
</div>