Actions

API15

JFolder/create

From Joomla! Documentation

< API15:JFolder
Revision as of 12:19, 12 May 2013 by JoomlaWikiBot (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Replacement filing cabinet.png
This Namespace has been archived - Please Do Not Edit or Create Pages in this namespace. Pages contain information for a Joomla! version which is no longer supported. It exists only as a historical reference, will not be improved and its content may be incomplete.

Contents

Description

Create a folder -- and all necessary parent folders.

[<! removed edit link to red link >]

<! removed transcluded page call, red link never existed >

Syntax

create($path= '', $mode=0755)
Parameter Name Default Value Description
$path A path to create from the base path.
$mode 0755 Directory permissions to set for folders created.

Returns

boolean True if successful.

Defined in

libraries/joomla/filesystem/folder.php

Importing

jimport( 'joomla.filesystem.folder' );

Source Body

function create($path = '', $mode = 0755)
{
        // Initialize variables
        jimport('joomla.client.helper');
        $ftpOptions = JClientHelper::getCredentials('ftp');
        static $nested = 0;
 
        // Check to make sure the path valid and clean
        $path = JPath::clean($path);
 
        // Check if parent dir exists
        $parent = dirname($path);
        if (!JFolder::exists($parent)) {
                // Prevent infinite loops!
                $nested++;
                if (($nested > 20) || ($parent == $path)) {
                        JError::raiseWarning(
                                'SOME_ERROR_CODE',
                                'JFolder::create: ' . JText::_('Infinite loop detected')
                        );
                        $nested--;
                        return false;
                }
 
                // Create the parent directory
                if (JFolder::create($parent, $mode) !== true) {
                        // JFolder::create throws an error
                        $nested--;
                        return false;
                }
 
                // OK, parent directory has been created
                $nested--;
        }
 
        // Check if dir already exists
        if (JFolder::exists($path)) {
                return true;
        }
 
        // Check for safe mode
        if ($ftpOptions['enabled'] == 1) {
                // Connect the FTP client
                jimport('joomla.client.ftp');
                $ftp = &JFTP::getInstance(
                        $ftpOptions['host'], $ftpOptions['port'], null,
                        $ftpOptions['user'], $ftpOptions['pass']
                );
 
                // Translate path to FTP path
                $path = JPath::clean(str_replace(JPATH_ROOT, $ftpOptions['root'], $path), '/');
                $ret = $ftp->mkdir($path);
                $ftp->chmod($path, $mode);
        } else {
                // We need to get and explode the open_basedir paths
                $obd = ini_get('open_basedir');
 
                // If open_basedir is set we need to get the open_basedir that the path is in
                if ($obd != null)
                {
                        if (JPATH_ISWIN) {
                                $obdSeparator = ";";
                        } else {
                                $obdSeparator = ":";
                        }
                        // Create the array of open_basedir paths
                        $obdArray = explode($obdSeparator, $obd);
                        $inBaseDir = false;
                        // Iterate through open_basedir paths looking for a match
                        foreach ($obdArray as $test) {
                                $test = JPath::clean($test);
                                if (strpos($path, $test) === 0) {
                                        $obdpath = $test;
                                        $inBaseDir = true;
                                        break;
                                }
                        }
                        if ($inBaseDir == false) {
                                // Return false for JFolder::create because the path to be created is not in open_basedir
                                JError::raiseWarning(
                                        'SOME_ERROR_CODE',
                                        'JFolder::create: ' . JText::_('Path not in open_basedir paths')
                                );
                                return false;
                        }
                }
 
                // First set umask
                $origmask = @umask(0);
 
                // Create the path
                if (!$ret = @mkdir($path, $mode)) {
                        @umask($origmask);
                        JError::raiseWarning(
                                'SOME_ERROR_CODE',
                                'JFolder::create: ' . JText::_('Could not create directory'),
                                'Path: ' . $path
                        );
                        return false;
                }
 
                // Reset umask
                @umask($origmask);
        }
        return $ret;
}

[<! removed edit link to red link >] <! removed transcluded page call, red link never existed >

Examples

<CodeExamplesForm />