PHP fwrite new line

PHP fwrite new line

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

PHP fwrite new line

When writing multi-line data to a file using fwrite, the most common stumbling block isn't the function itself, but how you handle line terminators across different operating systems. You might see code that looks correct at first glance:

$user  = $_GET['username'];
$password=$_GET['password'];
$fh = fopen("file.txt","a+");
fwrite($fh,$user."\n");
fwrite($fh,$password."\n");
fclose($fh);

The logic seems sound. You are opening the file in append mode, writing the username followed by a newline, then the password followed by another newline. However, this approach often results in data misalignment when the file is viewed on Windows or within certain text editors that expect different line ending sequences.

The Line Ending Problem

The core issue lies in the character sequence \n. In Unix-like environments (Linux, macOS), a single newline character (\n) signifies the end of a line. However, Windows traditionally expects the sequence \r\n (Carriage Return + Line Feed). If you write a file with only \n on a Windows machine, the editor might not render the newlines visually, or it might treat the entire content as a single massive line depending on the application settings.

Conversely, if your application runs on Linux but you strictly require Windows-style line endings for compatibility reasons (perhaps importing into an Excel sheet or a legacy system), writing \n alone can cause parsing errors downstream. The source material demonstrates a solution where the author manually concatenates a space between the username and password before appending the newline:

$user  = $_POST['username'];
$password = $_POST['password'].PHP_EOL;
$fh = fopen("file.txt","a+");
fwrite($fh,$user." ".$password); 
fclose($fh);

This approach is functional because it explicitly uses PHP_EOL. This constant automatically resolves to \n on Unix systems and \r\n on Windows, ensuring the output matches the underlying OS conventions. By appending the space (.) within the string before the line break, you guarantee that the two fields appear side-by-side on the same logical line, which aligns with the visual output shown in the requirements: Sebastian password.

Why PHP_EOL is Superior to Hardcoded \n

Using a hardcoded \n string literal is fragile in cross-platform development. It assumes a specific operating system environment. If you deploy your script from a local Mac to a Windows server, the file format generated will change unexpectedly unless you explicitly normalize the output or handle line endings at the filesystem layer.

By utilizing PHP_EOL, you offload that complexity to the runtime environment. The code becomes portable. When you combine this with the space character in your fwrite call, you ensure that the resulting text file contains exactly one newline per record, regardless of where the server is hosted. This makes it significantly easier to parse the file later, even if you are using a simple loop or a stream wrapper to read the data back into memory.

Security Considerations for Learning Projects

While the source material explicitly notes that storing passwords in plain text is not secure and limits this to learning purposes, it is worth reiterating why this architecture fails in production. The fwrite function writes raw bytes. It does not hash or encrypt the data. If an attacker gains read access to file.txt, they can extract every username and password pair immediately.

In a real-world scenario, you would never write the password directly to a file using fwrite. Instead, you would generate a cryptographic hash (like Argon2id or bcrypt) before writing it to storage. Even then, storing credentials in a flat text file is generally discouraged; a dedicated database with proper access controls and encryption at rest is the standard pattern.

However, for understanding file I/O mechanics, mastering the behavior of fwrite combined with PHP_EOL is a valuable exercise. It teaches you to respect platform-specific conventions rather than fighting them with hardcoded assumptions. When you need to append structured data that must remain readable across environments, dynamic line ending constants are the reliable choice.

Our network