我正在制作一个
Android应用程序,需要能够将文件推送到服务器上.
为此,我使用POST和fopen / fwrite,但此方法仅附加到文件并在写入文件之前使用unlink无效. (file_put_contents具有完全相同的效果)
这就是我到目前为止所拥有的
<?PHP $fileContent = $_POST['filecontent']; $relativePath = "/DatabaseFiles/SavedToDoLists/".$_POST['filename']; $savePath = $_SERVER["DOCUMENT_ROOT"].$relativePath; unlink($savePath); $file = fopen($savePath,"w"); fwrite($file,$fileContent); fclose($file); ?>
当我不尝试写入文件时,该文件将正确删除它自己,但如果我尝试写入它,它将附加.
谢谢,卢克.
使用wa打开和截断:
$file = fopen($savePath,"wa+");
w+: Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist,attempt to create it.
a+: Open for reading and writing; place the file pointer at the end of the file. If the file does not exist,attempt to create it.