To create a new file in PHP, you can use the file_put_contents()
function or fopen()
function along with fwrite()
. Here's an example using both methods:
Using file_put_contents()
function:
<?php
$file = 'path/to/newfile.txt';
$data = 'This is the content of the new file.';
file_put_contents($file, $data);
echo 'New file created.';
?>
Using fopen()
and fwrite()
functions:
<?php
$file = 'path/to/newfile.txt';
$data = 'This is the content of the new file.';
$handle = fopen($file, 'w');
fwrite($handle, $data);
fclose($handle);
echo 'New file created.';
?>
In both cases, make sure to replace 'path/to/newfile.txt'
with the actual path and filename you want to create. The 'w'
mode in fopen()
specifies that the file should be opened for writing.