Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP fwrite new line

Tags:

php

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); } 
like image 595
Dynamiite Avatar asked Feb 28 '13 07:02

Dynamiite


People also ask

Does fwrite add new line?

When writing to a file in PHP with fwrite , you can add a newline character with PHP_EOL. Using PHP_EOL instead of manually writing out a "\n" or "\r\n" is important to make your code as portable as possible.

How do I use fwrite in PHP?

PHP fwrite() Function$file = fopen("test. txt","w"); echo fwrite($file,"Hello World. Testing!");

What is the difference between fwrite () and File_put_contents ()?

fwrite() allows to write to file a byte or block of bytes at a time, file_put_content() writes the entire file in one go.... which is better? depends what you need to do! and on the volumes of data that you want to write! fwrite requires a handle while file_put_contents does not.

What does fwrite mean in PHP?

The fwrite() function in PHP is an inbuilt function which is used to write to an open file.


1 Answers

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 
like image 51
Dino Babu Avatar answered Sep 28 '22 18:09

Dino Babu