Skip to content

Instantly share code, notes, and snippets.

@danferth
Last active August 29, 2015 14:27
Show Gist options
  • Select an option

  • Save danferth/6c4f7f69126a99caa02c to your computer and use it in GitHub Desktop.

Select an option

Save danferth/6c4f7f69126a99caa02c to your computer and use it in GitHub Desktop.
open and write to file with PHP

Open a file and write to it with PHP

snippet to open a file, write to it (with timestamp), then close file with PHP

More can be found at fopen docs

<?php
//set date time if not already done elsewhere
date_default_timezone_set('America/Los_Angeles');
//set date/time for entry
$time_stamp = date('m\-d\-Y\-h:iA');
//set file name and location
$log = "assets/logs/log.txt";
//open or create log.txt
//[see docs for more modes](http://php.net/manual/en/function.fopen.php)
$fp = fopen($log,"a+");
//append text and time stamp to log.txt (the double \n's are to give readable space inbetween entries.)
fwrite($fp, $time_stamp . "\n" . "hello world" . "\n\n");
//close log file
fclose($fp);
//You can also use unlink() to delete file if needed
unlink($fp)
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment