Create a dynamic directory folder in PHP with the example

Dynamic directory folder
Create a dynamic directory folder in PHP with the example

Problem: If you want to create a dynamic directory folder for your project. So many times we have lots of user data in one folder, but we can not arrange properly if we required to search any images and doc for requirement then our lots of time waist to search. What can do for this data arrangement?


Solution: WeWebtech web source code provides you fully dynamic directory folder code for your project, this code is created dynamic years, months, and day according to manage your project uploaded data.

function create_dynamic_directory_folder(){
$myid=$this->session->userdata('id');
$my_fname=$this->session->userdata('user_name');
$my_lname=$this->session->userdata('last_name');
//make year directory if not exist
    if (!is_dir('./uploads/' . date("Y"))) {

        mkdir('./uploads/' . date("Y"), 0777, true);
        $year = './uploads/' . date("Y");
        touch("$year/index.html");
    }
    //make month directory if not exist
    if (!is_dir('./uploads/' . date("Y"))) {

        mkdir('./uploads/' . date("M"), 0777, true);
        $month = './uploads/' . date("M");
        touch("$month/index.html");
    }
    //make user directory if not exist
    if (!is_dir('./uploads/' . date("Y") . '/' . date("M") . '/' . $myid . '_' . $my_fname . '_' . $my_lname)) {

        mkdir('./uploads/' . date("Y") . '/' . date("M") . '/' . $myid . '_' . $my_fname . '_' . $my_lname, 0777, true);
        $userdir = './uploads/' . date("Y") . '/' . date("M") . '/' . $myid . '_' . $my_fname . '_' . $my_lname;
        touch("$userdir/index.html");
    }
//check image directory
  $imagedir = './uploads/' . date("Y") . '/' . date("M") . '/' . $myid . '_' . $my_fname . '_' . $my_lname . '/Wallpaper';

  if (!is_dir('./uplodsa/' . date("Y") . '/' . date("M") . '/' . $myid . '_' . $my_fname . '_' . $my_lname . '/Wallpaper')) {

      mkdir('./uploads/' . date("Y") . '/' . date("M") . '/' . $myid . '_' . $my_fname . '_' . $my_lname . '/Wallpaper', 0777, true);
      $imagedir = './uploads/' . date("Y") . '/' . date("M") . '/' . $myid . '_' . $my_fname . '_' . $my_lname . '/Wallpaper';
      touch("$imagedir/index.html");
  }
}

You can change parameters according to requirements. This code is tested on CodeIgniter projects.

Thanks for your reading this article, if this article is helpful for your project. So please comment on the same and share your friend’s circle.

Please follow and like us:
error

Leave a Reply

Your email address will not be published. Required fields are marked *