/    Sign up×
Community /Pin to ProfileBookmark

update string from text file to another text file ?

Hello @ll

this is my first Q in forum ?

Ok, I have a text file on my android device which I send to server every 2 min’s

text file contain’s only 1 ( single line ) filename gps.txt

that single line in text file look’s like this 45:16.2432432;19:2465467;12;4

in a server directory I have filename gps.txt ( in that file I store data )
curently I use this php on my server ( named upload_file.php )

[code=php]<?php

$file_path = “uploads/”;

$file_path = $file_path . basename( $_FILES[‘uploaded_file’][‘name’]);
if(move_uploaded_file($_FILES[‘uploaded_file’][‘tmp_name’], $file_path)) {
echo “success”;
} else{
echo “fail”;
}
?>
[/code]

but it only overwrite existing file!!!

how to modifty my existing file so that when I send new gps.txt with single line to add that line in gps.txt which is already on server ( not to overwrite it )

B.R.

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 16.2015 — [code=php]
<?php
$result = 'fail';
$newText = trim(file_get_contents($_FILES['uploaded_file']['tmp_name']));
if($newText !== '') {
// [a]ppend mode:
$fh = fopen('uploads/'.basename($_FILES['uploaded_file']['name'], 'a');
if(fputs($fh, $newText.PHP_EOL)) {
$result = 'success';
}
}
echo $result;
[/code]
Copy linkTweet thisAlerts:
@GShadoWauthorMay 16.2015 — I just tryed and it's not working...
Copy linkTweet thisAlerts:
@NogDogMay 16.2015 — Any error messages? If not...
[code=php]
<?php
ini_set('display_errors', true);
error_reporting(E_ALL);
// rest of code...
[/code]
Copy linkTweet thisAlerts:
@rootMay 16.2015 — Theres also an excellent sticky by a member in the post uploads field... Should take a look at that, very good practical working uploader.
Copy linkTweet thisAlerts:
@GShadoWauthorMay 18.2015 — I have solve that problem like this

[CODE]<?php

//expected data: 45:16.2432432;19:2465467;12;4

//file_path to the saved file.
$file_path = 'uploads/';
//file name of the temp file:
$file_name = $_FILES['uploaded_file']['tmp_name'];
//name of the saved file.
$new_file = $_FILES['uploaded_file']['name']; // or set this variable to 'gps.txt'

//if temp file doesn't exist.
if(!file_exists($file_name)) {
exit('The requested file was not uploaded.');
}

//if the file exists, then get it's contents into a string.
$contents = file_get_contents($file_name);
//test for expected data.
$contents = preg_replace('/[^0-9:.;]/','',$contents);
//append it to the saved file, testing to make sure it saved.
if(file_put_contents($file_path . $new_file,$contents . PHP_EOL, FILE_APPEND) !== FALSE) {
echo 'success.';
}
[/CODE]


[B]but I have now another problem[/B]

I need help, what if I send same text, just last number increasing so it will look like this



old 45:16.5555;18:50.83722;1;19:30:35;17052015;1;0

new 45:16.5555;18:50.83722;1;19:30:35;17052015;1;1



can I replace that same line with new until last number reaches 5, when reaches 5 then delete whole line



example:



current line on server is:

45:16.5555;18:50.83722;1;19:30:35;17052015;1;0



I send new line which is exactly same except last value ( 1 )

45:16.5555;18:50.83722;1;19:30:35;17052015;1;1 <- update existing line

45:16.5555;18:50.83722;1;19:30:35;17052015;1;2 <- update existing line

45:16.5555;18:50.83722;1;19:30:35;17052015;1;3 <- update existing line

45:16.5555;18:50.83722;1;19:30:35;17052015;1;4 <- update existing line

45:16.5555;18:50.83722;1;19:30:35;17052015;1;5 <- delete line from file



update until some line reach last value 5 and then delete it



Anyone can help modify above script ?
Copy linkTweet thisAlerts:
@NogDogMay 18.2015 — Can you? Yes, but it becomes more complicated, and as the file grows, it will take longer to process. At that point, I'd seriously consider using a database to record this, as then you'd just need a bit of query logic to update the latest record if applicable, otherwise inserting a new record.
Copy linkTweet thisAlerts:
@GShadoWauthorMay 18.2015 — It will be no more than 100 lines, is that too much to process ?
Copy linkTweet thisAlerts:
@NogDogMay 19.2015 — You can load the file into an array with the file() function:
[code=php]
$logArray = file('path/to/file.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
[/code]

Then you can loop through that file and look for matches, make updates to the array if found, or else add the new line to the end of the array (array_push()). Then write the modified array back to the file:
[code=php]
file_put_contents('path/to/file.txt', implode(PHP_EOL, $logArray);
[/code]

However, if more than one user/process could possibly call this at the same time, you might want to look into using flock() to avoid race conditions.
×

Success!

Help @GShadoW spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...