Skip to content

Instantly share code, notes, and snippets.

@tranchausky
Created July 25, 2024 04:43
Show Gist options
  • Save tranchausky/d977b24611bcc36fc4777750f3c240a3 to your computer and use it in GitHub Desktop.
Save tranchausky/d977b24611bcc36fc4777750f3c240a3 to your computer and use it in GitHub Desktop.
php function log file \logs\idkey\2024-07.txt
<?php
$folderLog= 'logs/';
$key = 'idkey';
$str = 'this is message to file';
logFile($folderLog, $key, $str);
function logFile($folderLog, $keyFolder, $stringToAppend, $formatFile =''){
if(!file_exists($folderLog)){
mkdir($folderLog);
}
//$keyFolder = 'atkey1/';
$keyFolder .='/';
if(!file_exists($folderLog.$keyFolder)){
mkdir($folderLog.$keyFolder);
}
if(empty($formatFile)){
$formatFile = date('Y-m');
}
$file = $folderLog.$keyFolder.$formatFile.'.txt';
//$stringToAppend = "This is the string to append.\n";
$stringToAppend .= '\n';
if(!file_exists($file)){
if (file_put_contents($file, $stringToAppend, 0) === false) {
echo "Error: Could not write to file first.";
}
}else{
if (file_put_contents($file, $stringToAppend, FILE_APPEND) === false) {
echo "Error: Could not write to the file.";
}
}
echo 'Done';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment