PHP - Write into a file with PHP
4 posts
Page 1 of 1
heey guys!
Let's start!
Create a code to write a file and open it in php,
let's start with the code:
see that is simple!
Let's start!
Create a code to write a file and open it in php,
let's start with the code:
Code: Select all
Done! lmao; $fileName = 'test.txt';
$file = fopen($fileName,'w');
if ($file === FALSE) {
echo "Can’t open file!";
}
$text = "This is a simple message" . PHP_EOL;
fwrite($file, $text);
fwrite($file, "Other text" . PHP_EOL);
fclose($file);
see that is simple!
This is a great code for some people to get the hang of this.
thanks for sharing.

Just so you know; you can change this:
Code: Select all
to
if ($file === FALSE) {
echo "Can’t open file!";
}
Code: Select all
or
if(!$file) echo "Can’t open file!";
Code: Select all
Here is my re-writting version:
$file = fopen($fileName,'w') or die('Can\'t open file!' . chr(10));
Code: Select all
$fileName = 'test.txt';
$file = @fopen($fileName,'a') or die('Can\'t open/create file!' . chr(10));;
$text = "This is a simple message" . PHP_EOL;
@fwrite($file, $text) or print('Failed to write to file.' . chr(10));
@fwrite($file, "Other text" . PHP_EOL) or print('Failed to write to file.' . chr(10));
@fclose($file);
4 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023