GShadoW Posted May 16, 2015 Share Posted May 16, 2015 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 fileCan someone help me, I dont know nothing about php. B.R. Quote Link to comment https://forums.phpfreaks.com/topic/296349-update-string-from-txt-file/ Share on other sites More sharing options...
QuickOldCar Posted May 16, 2015 Share Posted May 16, 2015 (edited) 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 May 16, 2015 by QuickOldCar Quote Link to comment https://forums.phpfreaks.com/topic/296349-update-string-from-txt-file/#findComment-1512019 Share on other sites More sharing options...
GShadoW Posted May 16, 2015 Author Share Posted May 16, 2015 (edited) i have placed your code in my php script but it's not working I have only renamed "filename.txt" to my gps.txt can you combine your code with my code in #1 post ? Edited May 16, 2015 by GShadoW Quote Link to comment https://forums.phpfreaks.com/topic/296349-update-string-from-txt-file/#findComment-1512020 Share on other sites More sharing options...
Barand Posted May 16, 2015 Share Posted May 16, 2015 or $data = file_get_contents($uploaded_file); file_put_contents($current_file, $data, FILE_APPEND); Quote Link to comment https://forums.phpfreaks.com/topic/296349-update-string-from-txt-file/#findComment-1512022 Share on other sites More sharing options...
jcbones Posted May 16, 2015 Share Posted May 16, 2015 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.'; } Quote Link to comment https://forums.phpfreaks.com/topic/296349-update-string-from-txt-file/#findComment-1512023 Share on other sites More sharing options...
GShadoW Posted May 16, 2015 Author Share Posted May 16, 2015 (edited) 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 May 16, 2015 by GShadoW Quote Link to comment https://forums.phpfreaks.com/topic/296349-update-string-from-txt-file/#findComment-1512025 Share on other sites More sharing options...
GShadoW Posted May 16, 2015 Author Share Posted May 16, 2015 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 Quote Link to comment https://forums.phpfreaks.com/topic/296349-update-string-from-txt-file/#findComment-1512026 Share on other sites More sharing options...
Ch0cu3r Posted May 16, 2015 Share Posted May 16, 2015 (edited) 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 May 16, 2015 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/296349-update-string-from-txt-file/#findComment-1512027 Share on other sites More sharing options...
GShadoW Posted May 16, 2015 Author Share Posted May 16, 2015 Thank you jcbones & Ch0cu3r now it works Quote Link to comment https://forums.phpfreaks.com/topic/296349-update-string-from-txt-file/#findComment-1512035 Share on other sites More sharing options...
GShadoW Posted May 17, 2015 Author Share Posted May 17, 2015 <?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 ? Quote Link to comment https://forums.phpfreaks.com/topic/296349-update-string-from-txt-file/#findComment-1512109 Share on other sites More sharing options...
Ch0cu3r Posted May 18, 2015 Share Posted May 18, 2015 Weird I cant seem to be able to send you a PM to inform you I have merged your topics seeing as they are similar. Also please do not bump old topics to "advertise" your new topics either. Quote Link to comment https://forums.phpfreaks.com/topic/296349-update-string-from-txt-file/#findComment-1512148 Share on other sites More sharing options...
GShadoW Posted May 18, 2015 Author Share Posted May 18, 2015 I have also want to send you pm regarding this, but I was also unable to send pm... Quote Link to comment https://forums.phpfreaks.com/topic/296349-update-string-from-txt-file/#findComment-1512151 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.