/** * Checks if the given directory and all contained files within it is writable by the current user. * @param string $dir The directory to check */ function is_writable_recursive($dir) { if (!is_writable($dir)) { throw new \Exception($dir . " is not writable."); } $folder = opendir($dir); while ($file = readdir($folder)) { if ($file != '.' && $file != '..') { if (!is_writable($dir . "/" . $file)) { closedir($folder); throw new \Exception($dir . "/" . $file . " is not writable."); } else { if (is_dir($dir . "/" . $file)) { if (!is_writable_recursive($dir . "/" . $file)) { closedir($folder); throw new \Exception($dir . "/" . $file . " is not writable."); } } } } } return true; }
/** * check if file or directory exists and is writeable, returns via parameters by reference * * @param string $path file or directory to check * @param int $type 0:undefined (invalid), 1:file, 2:directory * @param string $data to manipulate * @param string $base key for data manipulation * @param string $keyError key for error data * @return bool result of check (that it is writeable which implies existance) */ function check_PathWriteable($path, $type, &$data, $base, $keyError, $bRecursive = false) { $result = false; $data[$base . 'Present'] = 'Not Found'; $data[$base . 'Writable'] = ''; switch ($type) { case 1: $exists = is_file($path); break; case 2: $exists = is_dir($path); break; default: throw new Exception('Invalid type given.'); } if ($exists) { $data[$base . 'Present'] = 'Found'; if (!$bRecursive && is_writable($path) || $bRecursive && is_writable_recursive($path)) { $data[$base . 'Writable'] = 'Writable'; $result = true; } else { $data[$base . 'Writable'] = 'Unwritable'; } } $result || ($data[$keyError] = true); return $result; }