如何使用file_put_contents()将数据追加到文件? [英] How to append data to file using file_put_contents()?

查看:154
本文介绍了如何使用file_put_contents()将数据追加到文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个android应用程序,它从okhttp3发送多个数据,但是我找不到记录php中发送的所有数据的方法.我当前的日志仅包含最后一条记录(如下所示).我最好的猜测是php文件数据将被覆盖到最后一条记录.我如何记录发送的所有数据?是的,所有数据都从android应用程序中发送出去了...

I have an android app that sends multiple data from okhttp3 but i can't find a way to log all the data sent in php..My current log only houses the last record(Show below). My best guess is that the php file data is being overwritten until the last record..How can I log all the data sent? And yes all the data is being sent out from the android app...

index.php

index.php

if (isset($_POST))
 {
file_put_contents("post.log",print_r($_POST,true));
}

post.log示例

Sample 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 ;

You need to pass third parameter FILE_APPEND;

因此您的PHP代码将如下所示,

So your PHP code will look something like this,

if (isset($_POST))
 {
file_put_contents("post.log",print_r($_POST,true),FILE_APPEND);
}

FILE_APPEND 标志有助于将内容附加到文件而不是覆盖内容.

FILE_APPEND flag helps to append the content to the end of the file instead of overriding the content.

这篇关于如何使用file_put_contents()将数据追加到文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆