I am making an Android application that need to be able to push files onto a server.
For this I'm using POST
and fopen/fwrite
but this method only appends to the file and using unlink
before writing to the file has no effect. (file_put_contents
has the exact same effect)
This is what I have so far
<?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);
?>
The file will correctly delete its self when I don't try and write to it after but if I do try and write to it, it will appended.
Anyone got any suggestions on overwriting the file contents?
Thanks, Luke.
w
mode is "open file, truncate to zero length". If it was appending, you'd have to be opening ina
mode – Aisha