Files and Streams
Write File
file_put_contents writes a string and reports how many bytes were written.
Write File
write_file.php
<?php
$text = ;
$file = "php_write_file.txt";
$bytes = file_put_contents($file, $text);
$written = $bytes === false ? 0 : $bytes;
unlink($file);
echo "text=" . $text . "\n";
echo "written=" . $written . "\n";
<?php
$text = ;
$file = "php_write_file.txt";
$bytes = file_put_contents($file, $text);
$written = $bytes === false ? 0 : $bytes;
unlink($file);
echo "text=" . $text . "\n";
echo "written=" . $written . "\n";
<?php
$text = ;
$file = "php_write_file.txt";
$bytes = file_put_contents($file, $text);
$written = $bytes === false ? 0 : $bytes;
unlink($file);
echo "text=" . $text . "\n";
echo "written=" . $written . "\n";
write
For small text files, a single function call is enough to write content.