API16:JFolder/create
From Joomla! Documentation
Description
Create a folder -- and all necessary parent folders.
<! 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)
{
// Initialise 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 transcluded page call, red link never existed >
Examples
To create a subfolder(in this example: photoalbum) to images, write:
JFolder::create("images".DS."photoalbum");
As long as you execute this from the frontend your fine. If you like to make sure the folders are created under the site section regardless of where the command is executed, write:
JFolder::create(JPATH_SITE.DS."your path here");
For always under /administrator, write;
JFolder::create(JPATH_ADMINISTRATOR.DS."your path here");