轻松设置文件夹结构

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了轻松设置文件夹结构相关的知识,希望对你有一定的参考价值。

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.
  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. ?>

以上是关于轻松设置文件夹结构的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段5——HTML元素结构

轻松保存重复多用的代码片段

PHP 轻松的文件夹结构设置

轻松设置文件夹结构

如何从设置中获取数据并发送到此片段

14.VisualVM使用详解15.VisualVM堆查看器使用的内存不足19.class文件--文件结构--魔数20.文件结构--常量池21.文件结构访问标志(2个字节)22.类加载机制概(代码片段