PHP by example - How to write string to the file stream
Last week, I have a customer asked me on how to save a long XML string into a text file and put it into temporary directory, so that it can be attached into mail as an attachment. My answer is to use PHP function fwrite. fwrite writes the contents of string to the file stream pointed to by handle. Following is the example :
<?php $filename = "file.txt"; // file $content = "<xml><name>kelvin</name></xml>" // content if (!$file = fopen($filename, "w")) { // open the file for write echo "Cannot open file ($filename)."; exit; } if (fwrite($file, $content) === "FALSE") { // write content to the file echo "Cannot write to file ($filename)."; exit; } echo "Successfully wrote ($content) to the file ($filename)"; fclose($file); // close the file handle ?>
Job done!












Post a Comment