Question

I almost can't write php, and sorry if the question is silly.

This script generates text files like "2013-12-06_192.0.2.0.txt".
I want to change the behavior to generate text files in date folder. (for example, "./2013-12-06/192.0.2.0.txt") How can I do this?

<?php
header('Access-Control-Allow-Origin: *');
header("Content-type: text/html; charset=UTF-8");
mb_language("Japanese");
mb_internal_encoding("UTF-8");
$key=$_POST['key'];
$today = date("Y-m-d");
$logfile=$today."_".$_SERVER['REMOTE_ADDR'].".txt";
$fp = fopen($logfile, "a");
fwrite($fp, $key);
fclose($fp);
?>
Was it helpful?

Solution

You need to create the sub folder first.

// create sub directory (if not already existing)
if(!is_dir($today)) {
    mkdir($today);
}
// logfile path points to the subdirectory
$logfile=$today. "/" . $_SERVER['REMOTE_ADDR'].".txt";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top