2016-08-09 5 views
0

私はokhttp3から複数のデータを送信するアンドロイドアプリを持っていますが、私はPHPで送信されたすべてのデータを記録する方法を見つけることができません。私の現在のログは、最後のレコードのみを格納しています。私の最高の推測は、PHPファイルのデータが最後のレコードまで上書きされていることです。どのようにすべてのデータをログに記録できますか?そして、はい、すべてのデータがfile_put_contents()を使用してファイルにデータを追加する方法は?

のindex.php ... Androidアプリから送信されている

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 
) 
+0

どのようにfopen(wまたはモード)を使用しましたか? – coder

+0

@coder彼は 'file_put_contents()'を見ていません。 – RiggsFolly

+0

@coderデフォルトでは 'fopens'、 'fwrites'、 'fcloses'は必要ありません – Bmbariah

答えて

3

第3パラメータFILE_APPENDを渡す必要があります。

だからあなたのPHPコードは

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

FILE_APPENDフラグは、コンテンツを上書きするのではなく、 ファイルの末尾にコンテンツを追加するのに役立ちます、このようになります。

+0

完璧に働きました。ありがとう – Bmbariah

+0

@Aeoniaあなたの問題を解決したので、Alokの答えを選択するのを忘れないでください。 – BeetleJuice

+0

確かに。ちょうど6分待たなければならない。 – Bmbariah

1

FILE_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); 
?> 
関連する問題