I have a script that stores files into a directory. The function creates a new directory every 7 days based on the current date (2018>March>week1,2,3, etc.). It works fantastically, but I need to set the directory permissions to 777 otherwise I run into issues. See the code below.
static function initStorageFileDirectory() {
$filepath = 'storage/';
$year = date('Y');
$month = date('F');
$day = date('j');
$week = '';
$mode = 0777;
if (!is_dir($filepath . $year)) {
//create new folder
mkdir($filepath[$mode] . $year);
}
if (!is_dir($filepath . $year . "/" . $month)) {
//create new folder
mkdir($filepath[$mode] . "$year/$month");
}
if ($day > 0 && $day <= 7)
$week = 'week1';
elseif ($day > 7 && $day <= 14)
$week = 'week2';
elseif ($day > 14 && $day <= 21)
$week = 'week3';
elseif ($day > 21 && $day <= 28)
$week = 'week4';
else
$week = 'week5';
if (!is_dir($filepath . $year . "/" . $month . "/" . $week)) {
//create new folder
mkdir($filepath[$mode] . "$year/$month/$week");
}
$filepath = $filepath . $year . "/" . $month . "/" . $week . "/";
return $filepath;
}
As you can see, I set $mode. This probably isn't the best way to do this: with the [$mode] inserted it fails to create the directory altogether, but if I remove that bit of code from the mkdir($filepath.... it works great.