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