PlayItAgain Posted February 22, 2012 Share Posted February 22, 2012 Hi all, I have fwrite() writing to a text file called 'numbers.txt'. The text file content is "23". I learn't fwrite() from the manual, so did: f$fp = fopen('/opt/numbers.txt', 'w'); fwrite($fp, '1'); fclose ($fp); I have used fwrite() to try and make the file content "123", but fwrite() writes over the content of the file, resulting in it just containing "1". I couldn't find anywhere about writing into, rather over, and hoping someone could please help me out. Link to comment https://forums.phpfreaks.com/topic/257516-can-fwrite-write-into-rather-over/ Share on other sites More sharing options...
kicken Posted February 22, 2012 Share Posted February 22, 2012 Look at the manual for fopen to see what the different modes do. 'w' is: Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. In otherwords, it erases the entire contents of the file when you open it. There are other modes that allow writing which do not do this, such as 'r+' or 'a'. That said, there is no 'insert' mode where you can write data to the beginning (or middle) of a file without overwriting the data which is there. If you want to do that you have to read in all the existing data to a variable, truncate the file, write the new data, then re-write the old data. You can append data to the end of a file without having to touch the old data first. Use mode 'a' and then just fwrite the new data to put it at the end. Link to comment https://forums.phpfreaks.com/topic/257516-can-fwrite-write-into-rather-over/#findComment-1319891 Share on other sites More sharing options...
skwap Posted February 22, 2012 Share Posted February 22, 2012 Look at the manual for fopen to see what the different modes do. 'w' is: Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. In otherwords, it erases the entire contents of the file when you open it. There are other modes that allow writing which do not do this, such as 'r+' or 'a'. That said, there is no 'insert' mode where you can write data to the beginning (or middle) of a file without overwriting the data which is there. If you want to do that you have to read in all the existing data to a variable, truncate the file, write the new data, then re-write the old data. You can append data to the end of a file without having to touch the old data first. Use mode 'a' and then just fwrite the new data to put it at the end. yes you are right. using 'a' mode is the way to point the pointer at the end. But my question is how to do a break line ?? check my code. <?php $handle = fopen("test.txt",'a'); //Writing a new line. $result = fwrite($handle,"My name is sonu"); if($result) { echo "Done"; } else { echo "error"; } fclose($handle); ?> the file test.txt has already a line of text that is "my name is khan" & after executing the above code the text file contain a line like this my name is khanMy name is sonu but i want to make it like this my name is khan My name is sonu How it is possible ?? Link to comment https://forums.phpfreaks.com/topic/257516-can-fwrite-write-into-rather-over/#findComment-1319894 Share on other sites More sharing options...
kicken Posted February 22, 2012 Share Posted February 22, 2012 "\n" represents a new line. Just write that you where you want it to be. Link to comment https://forums.phpfreaks.com/topic/257516-can-fwrite-write-into-rather-over/#findComment-1319895 Share on other sites More sharing options...
skwap Posted February 22, 2012 Share Posted February 22, 2012 "\n" represents a new line. Just write that you where you want it to be. can you edit my code with it ?? to understand it more ? Link to comment https://forums.phpfreaks.com/topic/257516-can-fwrite-write-into-rather-over/#findComment-1319897 Share on other sites More sharing options...
PlayItAgain Posted February 22, 2012 Author Share Posted February 22, 2012 $fp = fopen('/opt/numbers.txt', 'w'); fwrite($fp, '1\n'); fclose ($fp); Gives me: 1\n23 Link to comment https://forums.phpfreaks.com/topic/257516-can-fwrite-write-into-rather-over/#findComment-1319899 Share on other sites More sharing options...
kicken Posted February 22, 2012 Share Posted February 22, 2012 <?php $handle = fopen("test.txt",'a'); //Writing a new line. $result = fwrite($handle,"\nMy name is sonu"); if($result) { echo "Done"; } else { echo "error"; } fclose($handle); ?> Link to comment https://forums.phpfreaks.com/topic/257516-can-fwrite-write-into-rather-over/#findComment-1319902 Share on other sites More sharing options...
PlayItAgain Posted February 22, 2012 Author Share Posted February 22, 2012 $fp = fopen('/opt/numbers.txt', 'w'); fwrite($fp, '1\n 2\n 3\n'); fclose ($fp); 1\n 2\n 3\n Link to comment https://forums.phpfreaks.com/topic/257516-can-fwrite-write-into-rather-over/#findComment-1319905 Share on other sites More sharing options...
xyph Posted February 22, 2012 Share Posted February 22, 2012 You need to use double quotes if you want \n to be parsed into a line-break. http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.double Link to comment https://forums.phpfreaks.com/topic/257516-can-fwrite-write-into-rather-over/#findComment-1319908 Share on other sites More sharing options...
PlayItAgain Posted February 22, 2012 Author Share Posted February 22, 2012 Double quotes don't work either: $fp = fopen('/opt/numbers.txt', 'w'); fwrite($fp, '1"\n" 2"\n" 3"\n"'); fclose ($fp); ... just gave me: 1"\n" 2"\n" 3"\n" Thanks but! Link to comment https://forums.phpfreaks.com/topic/257516-can-fwrite-write-into-rather-over/#findComment-1319910 Share on other sites More sharing options...
xyph Posted February 22, 2012 Share Posted February 22, 2012 Read the link I provided. echo 'the \n wont be parsed'; echo "the \n will be parsed"; Link to comment https://forums.phpfreaks.com/topic/257516-can-fwrite-write-into-rather-over/#findComment-1319912 Share on other sites More sharing options...
PlayItAgain Posted February 22, 2012 Author Share Posted February 22, 2012 I'm confused man. Link to comment https://forums.phpfreaks.com/topic/257516-can-fwrite-write-into-rather-over/#findComment-1319913 Share on other sites More sharing options...
PlayItAgain Posted February 22, 2012 Author Share Posted February 22, 2012 How can I use double quotes with this? I tried reversing the quotes to double quotes, but it doesn't work. This is why the above method doesn't work. fwrite($fp, '<?php session_start(); $id = "'.mysql_real_escape_string($_POST['id']).'";'); Link to comment https://forums.phpfreaks.com/topic/257516-can-fwrite-write-into-rather-over/#findComment-1319919 Share on other sites More sharing options...
PlayItAgain Posted February 22, 2012 Author Share Posted February 22, 2012 fuck this pc programming shit. im just gonna pay some cunt to do it. fuck dealing with this shit. Link to comment https://forums.phpfreaks.com/topic/257516-can-fwrite-write-into-rather-over/#findComment-1319925 Share on other sites More sharing options...
darkfreaks Posted February 22, 2012 Share Posted February 22, 2012 look into nl2br Link to comment https://forums.phpfreaks.com/topic/257516-can-fwrite-write-into-rather-over/#findComment-1319926 Share on other sites More sharing options...
PlayItAgain Posted February 22, 2012 Author Share Posted February 22, 2012 I am going to give this shit 1 last shot. I would appreciate it if people stop giving me fuckin manual pages. I can't understand them if it hasn't become obvious by now. I have so far been posting basic versions of what I am trying to do. To gain away any misunderstanding, I am gonna post my real code. You guys seem to know whats going on. So I don't think it will be a strain for your to read (I thought this maybe before which is why I made it basic). $fp = fopen('/opt/index.html', 'r+'); fwrite($fp, '<?php session_start(); $id = "'.mysql_real_escape_string($_POST['id']).'";'); fclose ($fp); This inserts into the following index.html: testusr', 'testpw') or exit("SERVER ERROR"); mysql_select_db('testdb', $link); $query = "SELECT * FROM products WHERE id = '$id'"; ... ... ... I am trying to make the outcome: <?php session_start(); $id = "21"; $link = mysql_connect('testusr', 'testpw') or exit("SERVER ERROR"); mysql_select_db('testdb', $link); $query = "SELECT * FROM products WHERE id = '$id'"; ... ... ... Instead, I get: <?php session_start(); $id = "21"; testusr', 'testpw') or exit("SERVER ERROR"); mysql_select_db('testdb', $link); $query = "SELECT * FROM products WHERE id = '$id'"; ... ... ... See how fwrite is eating into the code. I need this to stop. This is what all of this is about. So, I am trying to line break to get it going to the next line. But I can't! Its driving me nuts! Link to comment https://forums.phpfreaks.com/topic/257516-can-fwrite-write-into-rather-over/#findComment-1319932 Share on other sites More sharing options...
darkfreaks Posted February 22, 2012 Share Posted February 22, 2012 the line break would work if you used n12br $string = "this is a line /n this is a new line"; echo nl2br($string); //outputs "this is a line // this is a new line Link to comment https://forums.phpfreaks.com/topic/257516-can-fwrite-write-into-rather-over/#findComment-1319934 Share on other sites More sharing options...
Recommended Posts