加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

覆盖服务器上的文件(PHP)

发布时间:2020-05-26 01:47:55 所属栏目:PHP 来源:互联网
导读:我正在制作一个 Android应用程序,需要能够将文件推送到服务器上. 为此,我使用POST和fopen / fwrite,但此方法仅附加到文件并在写入文件之前使用unlink无效. (file_put_contents具有完全相同的效果) 这就是我到目前为止所拥有的 ?php$fileContent = $_POST[file

我正在制作一个 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+");

fopen

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.

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读