Using WordPress ‘debug_fopen()’ PHP function

The debug_fopen() WordPress PHP function opens a file handle for debugging purposes.

Usage

Here’s an example of using debug_fopen(). We are opening a file named ‘debug.log’ in write mode ‘w’, which means the file content will be truncated to zero length, and we will start writing at the beginning of the file.

$handle = debug_fopen('debug.log', 'w');

Parameters

  • $filename (string) – Required. The name of the file to be opened.
  • $mode (string) – Required. The type of access required to the stream. For example, ‘w’ for write mode, ‘r’ for read mode, ‘a’ for append mode.

More information

See WordPress Developer Resources: debug_fopen()
This function is used internally by WordPress for debugging purposes.

Examples

Open a file in write mode

This example opens a file named ‘debug.log’ in write mode.

$handle = debug_fopen('debug.log', 'w'); // Opens 'debug.log' in write mode

Open a file in read mode

This example opens a file named ‘read.log’ in read mode.

$handle = debug_fopen('read.log', 'r'); // Opens 'read.log' in read mode

Open a file in append mode

This example opens a file named ‘append.log’ in append mode.

$handle = debug_fopen('append.log', 'a'); // Opens 'append.log' in append mode

Open a file in write and read mode

This example opens a file named ‘write_read.log’ in write and read mode.

$handle = debug_fopen('write_read.log', 'w+'); // Opens 'write_read.log' in write and read mode

Open a file in append and read mode

This example opens a file named ‘append_read.log’ in append and read mode.

$handle = debug_fopen('append_read.log', 'a+'); // Opens 'append_read.log' in append and read mode