kakingho 0 Report post Posted July 17, 2006 <?php$fp=fopen("test.txt","a");fputs($fp,"ok");?> The above method is only to add "ok" at the end (DO NOT write on a new line)I want to know...How to add data on a new line and at the end of txt file??!thx~ Share this post Link to post Share on other sites
electriic ink 1 Report post Posted July 17, 2006 It would be : <?php $fp = fopen ("test.txt", "a"); fputs ($fp, "ok \nnewline");?> So yeah, basically, you just add \n but the one things I've found is that the \n must be in " NOT ' Share this post Link to post Share on other sites
galexcd 0 Report post Posted July 18, 2006 (edited) I have even noticed that in php strings can acctualy have a real carrage return! Instead of the \n u can just make the string on two lines! Sorta figured that one out on accident.Example: <?php$fp = fopen ("test.txt", "a"); fputs ($fp, "ok newline");?> Edited May 13, 2008 by galexcd (see edit history) Share this post Link to post Share on other sites
electron 0 Report post Posted July 18, 2006 Here is a better File writing function : <?php$filename = 'test.txt';$somecontent = "Add this to the file\n";// Let's make sure the file exists and is writable first.if (is_writable($filename)) { // In our example we're opening $filename in append mode. // The file pointer is at the bottom of the file hence // that's where $somecontent will go when we fwrite() it. if (!$handle = fopen($filename, 'a')) { echo "Cannot open file ($filename)"; exit; } // Write $somecontent to our opened file. if (fwrite($handle, $somecontent) === FALSE) { echo "Cannot write to file ($filename)"; exit; } echo "Success, wrote ($somecontent) to file ($filename)"; fclose($handle);} else { echo "The file $filename is not writable";}?> Also you should use the \n thing to hit enter in a file.But i dont think so it is necessary to put it in "" double quotes rather than single quotes .Best of luck Share this post Link to post Share on other sites
kakingho 0 Report post Posted July 18, 2006 ..thx a lot~~successful! Share this post Link to post Share on other sites
electron 0 Report post Posted July 19, 2006 You can also find a way to modify a existing file to change some of its values.e.g. If you have a global file that controls all your site settings and would like to modify it via the Admin Center you can use str_repalce function to do so.It is very effective for users.Good luck Share this post Link to post Share on other sites
Florisjuh 0 Report post Posted July 19, 2006 Thanks electron for the nice and very good usable PHP script, keep them coming ) Share this post Link to post Share on other sites
electron 0 Report post Posted July 20, 2006 What type of code or function do youy want ?I can help you.By the way you can also use preg_replace() fucntion if you are not so sure of the TEXT MATTER but no the Format of it e.g. a Hyperlink.But if you know the text then its advisable to use str_replace() function.Also for inserting any kind of quote - single or double - use a backslash to write it in the file.I love PHP Share this post Link to post Share on other sites
Spectre 0 Report post Posted July 20, 2006 A like PHP. I wouldn't go so far as to say I love it. Just a little expansion and clarification. preg_replace() uses the Perl-compatible regular expression engine built into PHP (as do all preg_ functions) to perform a regex search and replace operation on a given string, where the subject is searched for a pattern matching that given and replaced with the specified replacement (which can contain backreferences to the pattern matched). str_replace() is just a very simple static search and replace function for strings, where it replaces all given instances of one string within another with a specified value. preg_replace() is so much more powerful due to its harnessing PCRE - however, this means it is also slower, and should not be used unless required. Quotes only need to be escaped when that exact character is being used as a string delimiter. For instance, the string 'a"b"c' is perfectly legal, without the double quotes being escaped, as single quotes are acting as string delimiters - however, were the double quotes single quotes instead, they would need to be escaped, for example: 'a\'b\'c'. Within single quotes, no other characters will ever be escaped other than single quotes - so '\n' remains just that, whereas "\n" is the equivalent of chr(10) (and "\r" the equivalent of chr(13)). Variables will also be substituted within double quotes - eg. "the value is $variable" will become "the value is x" (assuming x is the value of the variable $variable - if that makes any sense at all). Because of these two factors, it is better to use single quotes where possible, even if this means concatenating strings delimited via two different sets of quotes (eg. 'start' . "\n" . 'end'), as it will always be faster. Although the speed difference generally isn't noticable, it can begin to slow down if large amounts of text is being processed, or if it is happening in rapid succession (eg. the code behind a very popular website), and it's simply a good programming practice to write clean, fast and efficient code. So anyway, that's my two and half cents worth. Share this post Link to post Share on other sites