-2

I have an Android application that posts String data to a PHP file on my server.

I am using a simple PHP GET to retrieve the data, but currently am not doing anything with it:

<?php
  $stringUploaded = $_GET['stringFromAndroid'];


?>

I would like the data that I receive to be entered into a text file, also on the server.

What is the best way for me to do this?

java123999
  • 6,974
  • 36
  • 77
  • 121
  • [file-put-contents](http://php.net/manual/en/function.file-put-contents.php) is a good way, always check documentation/google for the default best approach – Bonatti Nov 09 '15 at 10:29

2 Answers2

1

I would recommend for you to use the fwrite()-method

The PHP Code:

$file= fopen(yourFile.txt, "w");
fwrite($file, $stringUploaded);
fclose($file);

This should work :)

Lino
  • 19,604
  • 6
  • 47
  • 65
  • Thanks, what is the difference between this and file_put_contents ? – java123999 Nov 09 '15 at 09:52
  • @java123999 according to this [link](http://web.archive.org/web/20100109103851/http://balancedbraces.com/2008/06/12/fopen-fwrite-fclose-vs-file_put_contents/) fwrite is slightly faster – Lino Nov 09 '15 at 09:55
  • OK thanks, will this keep the file open so that I can send data from my Android app (e.g. every 10 seconds) and the php GET method will process it and send it to the text file? – java123999 Nov 09 '15 at 09:56
  • with `fopen()` you open the file, as long as you dont call the `fclose()`-Method you can do as many `fwrite()`as you wish, but you have to remember to not use the parameter "w" in `fopen()` because it would clear everthing in the file. use `"a"` instead :) – Lino Nov 09 '15 at 10:00
  • Do you think that file_put_contents would be a better approach? – java123999 Nov 09 '15 at 10:50
  • i've never worked with `file_puts_contents()` so i dunno what would be better, the only difference is just the speed, but thats not really noticable – Lino Nov 09 '15 at 11:03
  • @java123999 oh and pick an answer as the most useful one, so if anyone comes back on this topic they know the answer ;) – Lino Nov 09 '15 at 14:16
0

Disclaimer: just for future reference - this is a PHP question, don't mark it as Android, as it's irrelevant.

TL;DR: Here is a relevant answer from a different topic

Long answer:

You should really specify your PHP version, but to give you a generic answer, you can either use a general-purpose function like fopen + fwrite and fclose or a special function like file_put_contents($filename, $content).

Update: It's a good practice to write code in the most explicit way possible, so that the next person can understand it quickly. If your intent is to dump some data in a text file, use file_put_contents, and you will make the said intent explicit. Performance-wise it's the exact same thing - check the manual

Community
  • 1
  • 1
Seeker89
  • 282
  • 1
  • 2
  • 9
  • Thanks, will keep it in mind, which method would you suggest? – java123999 Nov 09 '15 at 10:00
  • I need to keep the file open so that I can send data from my Android app (e.g. every 10 seconds) and the php GET method will process it and send it to the text file? – java123999 Nov 09 '15 at 10:01
  • It's a good practice to write code in the most explicit way possible, so that the next person can understand it quickly. If your intent is to dump some data in a text file, use `file_put_contents`, and you will make the said intent explicit. Performance-wise it's the exact same thing - check http://php.net/manual/en/function.file-put-contents.php – Seeker89 Nov 09 '15 at 10:04
  • `I need to keep the file open so that I can send data from my Android app (e.g. every 10 seconds) and the php GET method will process it and send it to the text file?` This will not work - assuming that you are using a standard LAMP stack with Apache or something similar, PHP scripts are executed on each request - read, executed, closed, so you cant' share an open file between requests. – Seeker89 Nov 09 '15 at 10:06
  • Anyway, you probably don't want to write to a file directly anyway. Check a simple local database like [sqlite](http://www.tutorialspoint.com/sqlite/sqlite_php.htm) – Seeker89 Nov 09 '15 at 10:14