轻松设置文件夹结构
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.
<?php /** * Recursive function for creating folder structures * @param string Path to directory where $dir_structure should start creating dirs * @param array Associative array describing directory structure. * @param integer Optional file mode to create and check folders for. Defaults to 0755 */ function create_folder_structure($base_path, $structure, $file_mode = 0755) { foreach ($structure as $key => $value) { // value can be either the directory name or an array containing deeper directory structure $path_to_dir = $base_path.'/'.$dir_name; // create directory or set permissions else throw new Exception(sprintf('Cannot create or set file mode for directory "%s" to %d', $path_to_dir, $file_mode)); // create any deeper directory structure recursively create_folder_structure($path_to_dir, $value, $file_mode); } } /** * Example * Creates a folder structure in the already existing folder 'storage' * The folder structure will look like this: * storage/ * storage/images/ * storage/images/thumbnails/ * storage/images/thumbnails/large/ * storage/images/thumbnails/small/ * storage/images/portraits/ * storage/favorites/ * storage/tmp/ * storage/tmp/uploads/ * storage/tmp/edit/ * storage/files/ * * All of the folders will have file mode 0755. Pretty simple right? */ ( ( ( 'large', 'small' ), 'portraits' ), 'favorites', ( 'uploads', 'edit' ), 'files' ); try { create_folder_structure('storage', $arr_structure); } catch (Exception $e) { print $e->getMessage(); } ?>
以上是关于轻松设置文件夹结构的主要内容,如果未能解决你的问题,请参考以下文章
14.VisualVM使用详解15.VisualVM堆查看器使用的内存不足19.class文件--文件结构--魔数20.文件结构--常量池21.文件结构访问标志(2个字节)22.类加载机制概(代码片段