Recipe 24.10. Making New Directories

24.10.1. Problem

You want to create a directory.

24.10.2. Solution

Use mkdir( ), as in Example 24-25.

Making a directory

<?php mkdir('/tmp/apples',0777) or die($php_errormsg); ?>

24.10.3. Discussion

The second argument to mkdir( ) is the permission mode for the new directory, which must be an octal number. The current umask is taken away from this permission value to create the permissions for the new directory. So, if the current umask is 0002, calling mkdir('/tmp/apples',0777) sets the permissions on the resulting directory to 0775 (user and group can read, write, and execute; others can only read and execute).

By default, mkdir( ) only creates a directory if its parent exists. For example, if /usr/local/images doesn't exist, you can't create /usr/local/images/puppies. To create a directory and its parents, pass true as a third argument to mkdir( ). This makes the function act recursively to create any missing parent directories.

24.10.4. See Also

Documentation on mkdir( ) at

PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: