saftutorials.com
PHP FILE CREATE/WRITE

The PHP fopen() function is also used for creating the file if the file does not exist. The first parameter is the file name and second is a purpose of file open means which mode you want to open.


Syntax of fopen() function

fopen(filename, mode) - The first parameter is the file name and second is a purpose of file open means which mode you want to open


An example of PHP fopen() function for Create or Write a file.

<html>
<head>
<title>Reading a file</title>
</head>
<body>
<?php
$file_name = "test.txt";
$file = fopen( $file_name, "w" );
if( $file == false ) {
echo ( "Error" );
exit();
}
fwrite( $file, "This is a simple text for testing file to open and write\n" );
fclose( $file );
$file = fopen( $file_name, "r" );
$file_text = fread( $file, $file_size );
fclose( $file );
echo ( "<h2>File size : ".$file_size." bytes</h2>" );
echo ( "<p>$file_text</p>" );
?>
</body>
</html>

The above code produce the following results

Output : This is a simple text for testing file to open and write


Find us on

Facebook Google Plus Twitter Pinterest YouTube

web statistic