Revision: 23559
Updated Code
at April 9, 2010 02:44 by chlab
Updated Code
<?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) { if (!file_exists($base_path)) throw new Exception(sprintf('Directory "%s" does not exist', $base_path)); // value can be either the directory name or an array containing deeper directory structure $dir_name = is_string($key) ? $key : $value; $path_to_dir = $base_path.'/'.$dir_name; // create directory or set permissions if (!file_exists($path_to_dir)) mkdir($path_to_dir, $file_mode); else chmod($path_to_dir, $file_mode); if (!file_exists($path_to_dir) || !is_readable($path_to_dir) || !is_writeable($path_to_dir)) 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 if (is_array($value)) 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? */ $arr_structure = array ( 'images' => array ( 'thumbnails' => array ( 'large', 'small' ), 'portraits' ), 'favorites', 'tmp' => array ( 'uploads', 'edit' ), 'files' ); try { create_folder_structure('storage', $arr_structure); } catch (Exception $e) { print $e->getMessage(); } ?>
Revision: 23558
Updated Code
at March 2, 2010 02:58 by chlab
Updated Code
<?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) { if (!file_exists($base_path)) throw new Exception(sprintf('Directory "%s" does not exist', $base_path)); // value can be either the directory name or an array containing deeper directory structure $dir_name = is_string($key) ? $key : $value; $path_to_dir = $base_path.'/'.$dir_name; // create directory or set permissions if (!file_exists($path_to_dir)) mkdir($path_to_dir, $file_mode); else chmod($path_to_dir, $file_mode); if (!is_readable($path_to_dir) || !is_writeable($path_to_dir)) throw new Exception(sprintf('Cannot read or write directory "%s"', $path_to_dir)); // create any deeper directory structure recursively if (is_array($value)) 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? */ $arr_structure = array ( 'images' => array ( 'thumbnails' => array ( 'large', 'small' ), 'portraits' ), 'favorites', 'tmp' => array ( 'uploads', 'edit' ), 'files' ); try { create_folder_structure('storage', $arr_structure); } catch (Exception $e) { print $e->getMessage(); } ?>
Revision: 23557
Updated Code
at March 2, 2010 02:57 by chlab
Updated Code
<?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) { if (!file_exists($base_path)) throw new Exception(sprintf('Directory "%s" does not exist', $base_path)); // value can be either the directory name or an array containing deeper directory structure $dir_name = is_string($key) ? $key : $value; $path_to_dir = $base_path.'/'.$dir_name; // create directory or set permissions if (!file_exists($path_to_dir)) mkdir($path_to_dir, $file_mode); else chmod($path_to_dir, $file_mode); if (!is_readable($path_to_dir) || !is_writeable($path_to_dir)) throw new Exception(sprintf('Cannot read or write directory "%s"', $path_to_dir)); // create any deeper directory structure recursively if (is_array($value)) 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? */ $arr_structure = array ( 'images' => array ( 'thumbnails' => array ( 'large', 'small' ), 'portraits' ), 'favorites', 'tmp' => array ( 'uploads', 'edit' ), 'files' ); try { create_folder_structure('storage', $arr_structure); } catch (Exception $e) { print $e->getMessage(); } ?>
Revision: 23556
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at February 9, 2010 09:34 by chlab
Initial Code
<?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) { if (!file_exists($base_path)) throw new Exception(sprintf('Directory "%s" does not exist', $base_path)); // value can be either the directory name or an array containing deeper directory structure $dir_name = is_string($key) ? $key : $value; $path_to_dir = $base_path.'/'.$dir_name; // create directory or set permissions if (!file_exists($path_to_dir)) mkdir($path_to_dir, $file_mode); else chmod($path_to_dir, $file_mode); if (!is_readable($path_to_dir) || !is_writeable($path_to_dir)) throw new Exception(sprintf('Cannot read or write directory "%s"', $path_to_dir)); // create any deeper directory structure recursively if (is_array($value)) create_folder_structure($path_to_dir, $value, $file_mode); } } // example: create a folder structure in the already existing folder 'storage' $arr_structure = array ( 'images' => array ( 'thumbnails' => array ( 'large', 'small' ), 'portraits' ), 'favorites', 'tmp' => array ( 'uploads', 'edit' ), 'files' ); try { create_folder_structure('storage', $arr_structure); } catch (Exception $e) { print $e->getMessage(); } ?>
Initial URL
http://www.chlab.ch/
Initial Description
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.
Initial Title
Easy folder structure set up
Initial Tags
file, directory
Initial Language
PHP