Creating a file using php

How to create a file in php?

Explanation

Step 1:
Creating a new file in php is done using the function fopen().
Example: fopen('filename','w');
To create a file using php
a) call the function fopen with two required arguments.
b) the first argument should be the file name. The new file will be created with the file name under the current(same) directory.
c) the second argument should be 'w' indicating it as write mode.
Creating a new file under different Directory:
To create a file in a different directory we have to pass the full file path as the argument for file name.
Example: fopen('./images/filename.txt','w');
This will create a file named filename.txt under the directory "images/"
It is important to note that the directory specified in the path should exist.

PHP Topics


Ask Questions

Ask Question