PHP File create write - online php editor - Phptpoint.com

PHP File create write

PHP Summer Training/Internship with 2 Live Projects in Noida Fee is only 5500/-Click here to Book Your Seat.

/ins>

PHP create file and write contents

PHP comes with a couple of different ways to do this as well.

To write inside the file first file must be opened with mode.

How to Open a file in PHP

fopen( ) is used to open a file. You have to pass two parameters inside this function.

The first one is file name which you want to open and second is mode(purpose to open) of the file.

Syntax

Different types of File MODE

The file may be opened in one of the following modes:

ModesDescription
wWrite only. Opens and clears the contents of file; or creates a new file if it doesn’t exist
w+Read/Write. Opens and clears the contents of file; or creates a new file if it doesn’t exist
rRead only. Starts at the beginning of the file
r+Read/Write. Starts at the beginning of the file
aAppend. Opens and writes to the end of the file or creates a new file if it doesn’t exist
a+Read/Append. Preserves file content by writing to the end of the file
xWrite only. Creates a new file. Returns FALSE and an error if file already exists
x+Read/Write. Creates a new file. Returns FALSE and an error if file already exists

Write the contents inside file

1 file_put_contents()
The first is the file_put_contents()function, a close cousin of the file_get_contents() you will read about later.

file_put_contents() accepts a filename and path, together with the data to be written to the file, and then writes the latter to the former
Eg

If the file specified in the call to file_put_contents() already exists on disk,.file_put_contents() will overwrite it by default.
if instead, you’d prefer to preserve the file’s contents and simply append new data to it, and the special FILE_APPEND flag to your .file_put_contents() function call as a third argument.

Eg


Write the contents using fwrite( ) function

An alternative way to write data to a file is to create a file pointer with fopen( ), and then write data to the pointer using PHP’s fwrite( ) function.
Eg


Write the contents using fputs( ) function

An alternative way to write small data to a file is fputs( ) function.
Eg

Keep Updating yourself