Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function ensureDirectoryExists($filePath) {
- $pathSegments = array();
- $slashPosition = 0;
- $finished = false;
- while ($finished === false) {
- $slashPosition = mb_strpos($filePath, '/', $slashPosition + 1);
- if ($slashPosition === false) {
- $finished = true;
- }
- else {
- $pathSegments[] = mb_substr($filePath, 0, $slashPosition);
- }
- $maxPaths = 20;
- if (count($pathSegments) > $maxPaths) {
- throw new \Exception("Trying to create a directory more than $maxPaths deep. What is wrong with you?");
- }
- }
- foreach ($pathSegments as $segment) {
- if (file_exists($segment) === false) {
- //echo "Had to create directory $segment";
- $result = mkdir($segment);
- if ($result == false) {
- throw new \Exception("Failed to create segment [$segment] in ensureDirectoryExists($filePath).");
- }
- }
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement