Hey guys, I have made a funny dogs site and I am making an admin panel so that I can easily update things over the internet, but I would like to know how I can just add a line of data to a txt file, the text file containts many variables so it is something like this: <?php $var1 = Hello $var2 = Bye ?> (well not that exactly lol) but I want to know how I can just add one more variable by a form just before the ?> at the end so that it counts as a variable... or just something so that I can add <?php var3 = Lala ?> to the bottom of the file, just via form. Basically I need help in making something that can add a line of code to the bottom of a txt file, how do I do this? Thanks.
I found a mini script but as I want to add variables, I have to add " and ' but when I look at in on the txt file, it comes up with /" or /' how do I change this? Thanks.
Frist of all 'Hello' and 'Bye' must be put in quotes, becuase they are string, otherwise php will consider them constants, and issue warning if not found, put as this: $var = "Hello"; Now adding more to existing file: Method 1 $data = file_get_contents("filename.php"); $pos = strpos($data,"?>"); $new_data = '$var3 = "Go ahead";'; //this is new value to be added $datatowrite = substr($data,0,$pos+1)."\r\n$new_data\r\n?>"; $f = fopen("filename.php","w"); fwrite($f,$datatowrite); fclose($f); PHP: Method 2 $new_data = '$var3 = "Go ahead";'; //this is new value to be added $array = explode("\n",trim(file_get_contents("filename.php"))); $key = array_search("?", $array); if($key === FALSE) print "File does not have ?>"; else { $array[$key] = $new_data; $array[]= "?>"; $datatowrite = implode("\n",$array); $f = fopen("filename.php","w"); fwrite($f,$datatowrite); fclose($f); } PHP: I hope it helps.
I really don't know about this topic .............................please explain more about above topic
Here is an article on: Reading and writing files in php http://www.apitalk.com/document.php?id=1184208074_5 regards
\r\n seems used by some text editors. What if i remove it from the code in method 1? What is the effect?