Skip to content

Instantly share code, notes, and snippets.

@alancoleman
Last active December 23, 2015 03:29
Show Gist options
  • Save alancoleman/6573685 to your computer and use it in GitHub Desktop.
Save alancoleman/6573685 to your computer and use it in GitHub Desktop.
Function to create a file, add some content and write out to the browser.
<?php
// createFile function
function createFile($filename) {
// Declare content
$content = "Some content here";
$morecontent = "And some more content here";
// Add content to file
file_put_contents(
$filename,
"$content\n$morecontent"
);
// Message browser
echo $filename . " updated.<hr / >";
// Open file, read only
$file = fopen($filename, "r") or exit("Unable to open file!");
//Output a line of the file until the end is reached
while(!feof($file))
{
echo fgets($file). "<br / >";
}
fclose($file);
}
// Declare file name
$createFilename = "file.txt";
// Call function, taking $createFilename as an argument
createFile($createFilename);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment