Skip to content

Instantly share code, notes, and snippets.

@MehulBawadia
Created April 3, 2017 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MehulBawadia/7ad6ef8efb1e2148c0f1b93af7a256ae to your computer and use it in GitHub Desktop.
Save MehulBawadia/7ad6ef8efb1e2148c0f1b93af7a256ae to your computer and use it in GitHub Desktop.
A simple trait for creating a directory in PHP
<?php
/**
* A simple trait for creating a directory.
*
* It can be used anywhere in/by the application.. It is specially
* recommended to use this for creating directories of user based
* folders at the time of user registration.
*
* @author IamCrazyD <mehulbawadia@gmail.com>
* @package \ (Root)
* @version 1.0.0
*/
trait CreateDirectoryIfNotExist
{
/**
* Create the directory if the given path does not exist.
*
* @param string $path
* @return string
*/
private function createDirectoryIfNotExists($path)
{
$envType = env('APP_ENV');
$hostingType = env('HOSTING_TYPE');
if ($hostingType == 'shared') {
$path = base_path() . '/../public_html' . $path;
}
if ($hostingType == 'cloud') {
$path = public_path() . $path;
}
if (! file_exists($path)) {
mkdir($path, 0775, true);
}
return $path;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment