PHP Function to write to a file

Simple php function to write to a file, let you define the extension, file name and the content to write.


//function to create the file and write to it
function writeToFile($result, $filename, $extension) {

$myFile = $filename.".".$extension;

if(file_exists($myFile)){
unlink($myFile);
}
$fn = fopen($myFile, 'w') or die('Error opening the file');
fwrite($fn, $result);
fclose($fn);
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s