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.
This doesn't do what you think it does. It takes the character at index
$modefrom$filepath, appends$yearto it, and creates a directory at the result (without explicitly setting permissions). Since$filepathdoesn't have 512 characters in it (0777is 511 in octal),$filepath[$mode]returns an empty string (with an "Uninitialized string offset" notice) andmkdirtries to create a directory at$year.mkdirtakes multiple arguments, the second of which is the mode:But
mkdir's default mode is0777, so if the directory permissions end up being different, yourumaskis getting in the way. You can set yourumaskto allow0777permissions, but it's easier and (possibly) safer tochmodthe directory after creating it: