Difference between revisions of "JFolder/create"
From Joomla! Documentation
< API16:JFolder
Ravenheart (talk | contribs) |
Ravenheart (talk | contribs) m (→Examples) |
||
Line 152: | Line 152: | ||
To create a subfolder(in this example: photoalbum) to images, write: | To create a subfolder(in this example: photoalbum) to images, write: | ||
<source lang="php"> | <source lang="php"> | ||
− | JFolder::create("images.DS.photoalbum"); | + | JFolder::create("images".DS."photoalbum"); |
</source> | </source> | ||
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: | 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: |
Revision as of 02:36, 1 October 2011
The "API16" namespace is an archived namespace. This page contains information for a Joomla! version which is no longer supported. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.
Description[edit]
Create a folder -- and all necessary parent folders.
Template:Description:JFolder/create
Syntax[edit]
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[edit]
boolean True if successful.
Defined in[edit]
libraries/joomla/filesystem/folder.php
Importing[edit]
jimport( 'joomla.filesystem.folder' );
Source Body[edit]
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;
}
[Edit See Also] Template:SeeAlso:JFolder/create
Examples[edit]
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");