2016-08-09 2 views
0

나는 okhttp3에서 여러 데이터를 전송하는 안드로이드 애플 리케이션을 가지고 있지만, PHP에서 보낸 모든 데이터를 기록하는 방법을 찾을 수 없습니다. 내 현재 로그는 오직 아래의 마지막 기록 만 가지고 있습니다. 내 최고의 추측은 PHP 파일 데이터가 마지막 레코드까지 덮어 쓰여지고 있다는 것입니다. 어떻게 모든 데이터를 로그 할 수 있습니까? 그리고 네 모든 데이터는file_put_contents()를 사용하여 파일에 데이터를 추가하는 방법은 무엇입니까?

index.php에 ... 안드로이드 응용 프로그램으로부터 전송되는

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

세 번째 매개 변수 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); 
?> 
관련 문제