Jump to content

Recommended Posts

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 )

<?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";
    }
 ?>

but I only overwrite existing file!!!

what I need is scrip that will add new line without overwriting anything in that file
Can someone help me, I dont know nothing about php.

 

B.R.

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/296349-update-string-from-txt-file/
Share on other sites

You should check out the php manual and the tutorial

 

Here is a simple example can try.

<?php
//read a file
$my_file = "filename.txt";
if (file_exists($my_file)) {
    $data  = file($my_file);
    $total = count($data);
    echo "<br />Total lines: $total<br />";
    foreach ($data as $line) {
        $line = trim($line);
        echo "$line<br />";
    }
    
    //add a new line,note the a
    $write   = fopen($my_file, 'a+');
    //rewind($write);//append to top
    $message = "added a new line here\r\n";
    fputs($write, $message);
    fclose($write);
    
    /*
    //note the w, this will overwrite the entire contents
    $write = fopen($my_file, 'w');
    $message = "I just added this line and overwrote the file\r\n";
    fputs($write, $message);
    fclose($write);
    */
    
} else {
    echo "No file to display";
}
?> 
Edited by QuickOldCar

I'll bite:

 

<?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 =  sys_get_tmp_dir() . $_SERVER['uploaded_file']['tmp_name'];
//name of the saved file.
$new_file = $_SERVER['uploaded_file']['tmp_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.';
}

 

I'll bite:

<?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 =  sys_get_tmp_dir() . $_SERVER['uploaded_file']['tmp_name'];
//name of the saved file.
$new_file = $_SERVER['uploaded_file']['tmp_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.';
}

should I rename tmp_name to gps.txt ?

I have try and it not working

 

should I make some changes here ?

 

$file_name =  sys_get_tmp_dir() . $_SERVER['uploaded_file']['tmp_name'];

Edited by GShadoW

I send gps.txt from my mobile phone like this

Sub Button1_Click
Label1.Text=""
Label2.Text=""
Dim Path_Phone_Image As String
Path_Phone_Image = File.DirRootExternal & "/"  'OR  "/sdcard/
Dim name_image As String
name_image="gps.txt" 
Up.doFileUpload( ProgressBar1,Label1,Path_Phone_Image & name_image,Url_Php_Page)	
End Sub

my Url_Php_Page is located on my server www.xxxx.xxx/upload_file.php

 

and php look's like this

<?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";
    }
 ?>

it works to save gps.txt but it always overwrite it, not adding new line to existing text inside

EDIT. In jcbones code change lines 8, 9 and 10 to

$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'

You should not need to modify the code provided by jcbones. The code should append the contents of the uploaded file to gps.txt.

 

If the code is not working, then either the file is not being uploaded or it is unable to write to gps.txt. Check your servers error log to see if there are any errors.

Edited by Ch0cu3r
<?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']; // 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.';
}

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 ?

 

 

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.