Hướng dẫn php fwrite line break

Basically, I was trying to find a simple piece of code for a simple note taking thing in html, I want to type in an html input form, then that input be added on to a text file i found this,

Adding a text block to a text file:

Which works, but needs line breaks at the end of the output, such as if i wrote "test" and then "test" again, it outputs as "testtest" obviously, but I want two line breaks between them, like

"Test

Test"

I also want to add another input called "Subject" which will be the subject of the note,I'm open to any suggestions, if this code is wrong, or over complicated, then feel free to shorten it or just give me a entirely new one.

Thanks for any help

Terry.

I tried this but wont work

 

This is how it displays in my newfile.txt:

Mickey MouseMinnie Mouse`

but I want it like this:

Mickey Mouse
Minnie Mouse

William

3471 gold badge6 silver badges20 bronze badges

asked Oct 12, 2014 at 6:17

2

It is the best to use PHP_EOL. It's cross-platform and will automatically choose the correct newline character[s] for the platform PHP is running on.

$txt = "Goofy".PHP_EOL;

William

3471 gold badge6 silver badges20 bronze badges

answered Oct 12, 2014 at 6:21

Marcus RommelMarcus Rommel

1,1961 gold badge11 silver badges17 bronze badges

1

\r\n

add the carriage return in front.

answered Oct 12, 2014 at 6:24

1

 

if you want write \n in file use from file_put_contents[$File_Handle,$Text_For_Write,FILE_APPEND]

answered Feb 9, 2020 at 12:29

mamalmamal

1,45515 silver badges12 bronze badges

I'm trying to write username and password to a new line in a txt file. The output should be something like this in the txt file. I know this is not very secure but its just for learning purposes

Sebastian   password
John        hfsjaijn

This is what i have so far

if[isset[$_GET['register']]] //  
{
    $user  = $_GET['username'];
    $password=$_GET['password'];
    $fh = fopen["file.txt","a+"];
    fwrite[$fh,$user."\n"]; //write to txtfile
    fwrite[$fh,$password."\n"]; // write to txtfile
    fclose[$fh];
}

EDIT: Here's the solution for me:

if [isset[$_POST['register']]] {
   $user  = $_POST['username'];
   $password = $_POST['password'].PHP_EOL;
   $fh = fopen["file.txt","a+"];
   fwrite[$fh,$user." ".$password]; //write to txtfile
  
   fclose[$fh];
}

Martijn

15.5k4 gold badges36 silver badges66 bronze badges

asked Feb 28, 2013 at 7:56

6

Use PHP_EOL which produces \r\n or \n

$data = 'my data' . PHP_EOL . 'my data';
$fp = fopen['my_file', 'a'];
fwrite[$fp, $data];
fclose[$fp];

// File output

my data
my data

candlejack

1,1692 gold badges21 silver badges50 bronze badges

answered Feb 28, 2013 at 8:03

Dino BabuDino Babu

5,7663 gold badges23 silver badges33 bronze badges

1

You append a newline to both the username and the password, i.e. the output would be something like

Sebastian
password
John
hfsjaijn

use fwrite[$fh,$user." ".$password."\n"]; instead to have them both on one line.
Or use fputcsv[] to write the data and fgetcsv[] to fetch it. This way you would at least avoid encoding problems like e.g. with $username='Charles, III';

...i.e. setting aside all the things that are wrong about storing plain passwords in plain files and using _GET for this type of operation [use _POST instead] ;-]

answered Feb 28, 2013 at 8:01

VolkerKVolkerK

94.3k20 gold badges162 silver badges225 bronze badges

2

fwrite[$handle, "
"."\r\n"];

Add this under

$password = $_POST['password'].PHP_EOL;

this. .

answered Dec 2, 2014 at 20:53

How about you store it like this? Maybe in username:password format, so

sebastion:password123
anotheruser:password321

Then you can use list[$username,$password] = explode[':',file_get_contents['users.txt']]; to parse the data on your end.

answered Jan 26, 2019 at 17:05

Recommended Posts

    • Share

Hi

I have this code:

Chủ Đề