replace string in line or delete ?

[php] <?php

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

//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’]; //

//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.’;
} [/php]

I send to server gps.text and with script above I only update existing gps.text on server with new line ( not overwriting existing file on server )

so expected line look like this: 45:16.5555;18:50.83722;1;19:30:35;17052015;1;0

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 ?

Well, GShadoW, anything is possible. You are not clear on exactly what you wish.

Do you mean you want to keep six copies of the file? If so, when the sixth copy is created,
delete the others? Or do you want to have them always six copies oldest to newest.

In your code, you check to see if the file exists. There are many ways to check for that. You can load
all filenames that start with the name not comparing the last number, or check six times for each of the
possible last numbers. If you wish to have five versions of the file, is the first one that ends with “;0” the
first one or the “oldest” one? If so, you would have to rename then and always write the newest data to
the one that ends with “;5”…

We can help you however you wish it to work. Should be easy to alter your code to do it.
Just fill us in a bit further on exactly what the six copies would be. Renaming them would be easy enough.
Delete #0, rename 1 as 0, rename 2 as 1, etc and store new data as #5… Or, whatever as needed which
depends on how you want it to work.

Let us know and we will help…

Sponsor our Newsletter | Privacy Policy | Terms of Service