Php 如何使用file_put_contents()将数据附加到文件?
我有一个android应用程序,可以从okhttp3发送多个数据,但我找不到一种方法来记录所有用php发送的数据。我当前的日志只包含最后一条记录(如下所示)。我最好的猜测是php文件数据被覆盖,直到最后一条记录。我如何记录所有发送的数据?是的,所有的数据都是从android应用程序发送的 index.phpPhp 如何使用file_put_contents()将数据附加到文件?,php,android,loops,foreach,Php,Android,Loops,Foreach,我有一个android应用程序,可以从okhttp3发送多个数据,但我找不到一种方法来记录所有用php发送的数据。我当前的日志只包含最后一条记录(如下所示)。我最好的猜测是php文件数据被覆盖,直到最后一条记录。我如何记录所有发送的数据?是的,所有的数据都是从android应用程序发送的 index.php if (isset($_POST)) { file_put_contents("post.log",print_r($_POST,true)); } 示例post.log Array
if (isset($_POST))
{
file_put_contents("post.log",print_r($_POST,true));
}
示例post.log
Array
(
[date] => 02 Aug, 12:22
[company] => Assert Ventures
[lattitude] => 32.8937542
[longitude] => -108.336584
[user_id] => Malboro
[photo_id] => 1
)
我想要什么
(
[date] => 02 Aug, 12:22
[company] => Three Ventures
[lattitude] => 302.8937542
[longitude] => -55.336584
[user_id] => Malboro
[photo_id] => 1
),
(
[date] => 02 Aug, 12:22
[company] => Two Ventures
[lattitude] => 153.8937542
[longitude] => -88.336584
[user_id] => Malboro
[photo_id] => 1
),
(
[date] => 02 Aug, 12:22
[company] => Assert Ventures
[lattitude] => 32.8937542
[longitude] => -108.336584
[user_id] => Malboro
[photo_id] => 1
)
您需要传递第三个参数
FILE_APPEND
所以你的PHP代码看起来像这样
if (isset($_POST))
{
file_put_contents("post.log",print_r($_POST,true),FILE_APPEND);
}
文件\u APPEND标志有助于将内容追加到文件末尾
文件,而不是覆盖内容
我认为你应该添加文件附加标志
<?php
$file = 'post.log';
// Add data to the file
$addData = print_r($_POST,true);
// Write the contents to the file,
// using the FILE_APPEND flag to append the content to the end of the file
// and the LOCK_EX flag to prevent anyone else writing to the file at the same time
file_put_contents($file, $addData, FILE_APPEND | LOCK_EX);
?>
你是如何使用fopen的(w或a模式)?@coder他没有看到文件放内容()
@coder不需要..文件放内容“fopens”、“fwrites”和“fclose”default@RiggsFolly我的bad@Aeonia在未来的日子里,它工作得很好。。Thanks@Aeonia别忘了选择Alok的答案,因为它解决了你的问题。