Easy folder structure set up


/ Published in: PHP
Save to your folder(s)

Here is a function you can use to automatically set up a complex folder structure with a specified file mode. All you have to do is create the "root" directory and pass an array describing the folder structure you would like to create in it. If the directory already contains a part of the desired directory structure, the script will set the specified file mode on every existing folder. There's an example how to use the function at the bottom of the snippet.

I use this to set up user home's. I tested this with PHP 5.2.11 on Apache running on Mac OS X. It should work on most Linux servers. It won't work on Windows.

This function should be easy to modify for your needs or integrate into a class. Have fun.


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. /**
  4.  * Recursive function for creating folder structures
  5.  * @param string Path to directory where $dir_structure should start creating dirs
  6.  * @param array Associative array describing directory structure.
  7.  * @param integer Optional file mode to create and check folders for. Defaults to 0755
  8.  */
  9. function create_folder_structure($base_path, $structure, $file_mode = 0755)
  10. {
  11. foreach ($structure as $key => $value)
  12. {
  13. if (!file_exists($base_path))
  14. throw new Exception(sprintf('Directory "%s" does not exist', $base_path));
  15.  
  16. // value can be either the directory name or an array containing deeper directory structure
  17. $dir_name = is_string($key) ? $key : $value;
  18. $path_to_dir = $base_path.'/'.$dir_name;
  19.  
  20. // create directory or set permissions
  21. if (!file_exists($path_to_dir))
  22. mkdir($path_to_dir, $file_mode);
  23. else
  24. chmod($path_to_dir, $file_mode);
  25.  
  26. if (!file_exists($path_to_dir) || !is_readable($path_to_dir) || !is_writeable($path_to_dir))
  27. throw new Exception(sprintf('Cannot create or set file mode for directory "%s" to %d', $path_to_dir, $file_mode));
  28.  
  29. // create any deeper directory structure recursively
  30. if (is_array($value))
  31. create_folder_structure($path_to_dir, $value, $file_mode);
  32. }
  33. }
  34.  
  35. /**
  36.  * Example
  37.  * Creates a folder structure in the already existing folder 'storage'
  38.  * The folder structure will look like this:
  39.  * storage/
  40.  * storage/images/
  41.  * storage/images/thumbnails/
  42.  * storage/images/thumbnails/large/
  43.  * storage/images/thumbnails/small/
  44.  * storage/images/portraits/
  45.  * storage/favorites/
  46.  * storage/tmp/
  47.  * storage/tmp/uploads/
  48.  * storage/tmp/edit/
  49.  * storage/files/
  50.  *
  51.  * All of the folders will have file mode 0755. Pretty simple right?
  52.  */
  53.  
  54. $arr_structure = array
  55. (
  56. 'images' => array
  57. (
  58. 'thumbnails' => array
  59. (
  60. 'large',
  61. 'small'
  62. ),
  63. 'portraits'
  64. ),
  65. 'favorites',
  66. 'tmp' => array
  67. (
  68. 'uploads',
  69. 'edit'
  70. ),
  71. 'files'
  72. );
  73. try
  74. {
  75. create_folder_structure('storage', $arr_structure);
  76. }
  77. catch (Exception $e)
  78. {
  79. print $e->getMessage();
  80. }
  81.  
  82. ?>

URL: http://www.chlab.ch/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.