我有一些代码我正在使用一个字段的名称创建一个文件,然后打开该文件并从另一个文件中写入内容。这很好。
但是,当用户尝试提取此文件并更改信息并保存时,它不会覆盖该信息。
如果用户删除文件(有效),然后使用相同的名称重新创建它,但尝试输入新数据,它会创建新文件(与旧文件同名)但是它会保留旧信息和没有更新。我无法找出导致这种情况的原因。
我最初使用的是file_put_contents,我试图为fopen()使用不同的参数,但它似乎不起作用。他们可以创建和保存很好,但主要问题是他们无法编辑,因为它不会覆盖数据,他们无法删除文件(即使作为一种解决方法)并使用相同的名称重新创建它。
已编辑添加:当他们尝试保存信息时,不会打开文件并覆盖文件,而是创建第二个和新文件filename.html.html
// Put together the full path of the file we want to create
$FILENAME = $USER_DIRECTORY.'/'.$_POST['CodeDescription'].'.html';
$CODE = $_POST['Code'];
if( !is_file( $FILENAME ) ):
// Open the text file, write the contents, and close it.
$fp = fopen($FILENAME, "w+") or die("Couldn't open $FILENAME for
writing!");
fwrite($fp, $CODE) or die("Couldn't write values to file!");
endif;
header('Location: mywebsite.comsaved=1&file='.$FILENAME);
}
?>
答案 0 :(得分:2)
快速破解可能是尝试这个:
// Put together the full path of the file we want to create
$FILENAME = $USER_DIRECTORY.'/'.$_POST['CodeDescription'].'.html';
$CODE = $_POST['Code'];
//Delete file if exists.
if(is_file( $FILENAME)) {
unlink($FILENAME);
}
// Open the text file, write the contents, and close it.
$fp = fopen($FILENAME, "w+") or die("Couldn't open $FILENAME for
writing!");
fwrite($fp, $CODE) or die("Couldn't write values to file!");
header('Location: mywebsite.comsaved=1&file='.$FILENAME);
}