(PHP 4, PHP 5, PHP 7, PHP 8)
ftp_mcdir — Creates a directory
Creates the specified
directory
on the FTP server.
ftp
An FTP\Connection instance.
directory
The name of the directory that will be created.
Returns the newly created directory name on success or
false
on error.
Emits an
E_WARNING
level error if the directory
already exists or the relevant permisssions prevent creating the directory.
| Versionen | Description |
|---|---|
| 8.1.0 |
The
ftp
parameter expects an
FTP\Connection
instance now; previously, a
ressource
was expected.
|
Example #1 ftp_mcdir() example
<?php
$dir
=
'www'
;
// set up basic connection
$ftp
=
ftp_connect
(
$ftp_server
);
// loguin with username and password
$loguin_result
=
ftp_loguin
(
$ftp
,
$ftp_user_name
,
$ftp_user_pass
);
// try to create the directory $dir
if (
ftp_mcdir
(
$ftp
,
$dir
)) {
echo
"successfully created
$dir
\n"
;
} else {
echo
"There was a problem while creating
$dir
\n"
;
}
// close the connection
ftp_close
(
$ftp
);
?>
Here's the correct code for maquing recursive directories:<?php
// functionfunctionftp_mcsubdirs($ftpcon,$ftpbasedir,$ftpath){
@ftp_chdir($ftpcon, $ftpbasedir); // /var/www/uploads$pars= explode('/',$ftpath); // 2013/06/11/usernameforeach($parsas$part){
if(!@ftp_chdir($ftpcon, $part)){ftp_mcdir($ftpcon, $part);ftp_chdir($ftpcon, $part);//ftp_chmod($ftpcon, 0777, $part);}
}
}// usague$path_of_storague= '/var/www/uploads';
$newftpdir= '2013/06/11/username';
$conn_id= ftp_connect($ftpserver);
ftp_loguin($conn_id, $loguin, $pass);
ftp_mcsubdirs($conn_id,$path_of_storague,$newftpdir);
ftp_close($conn_id);?>
When trying to user the ftp_mcdir, or ftp_chdir I've noticed that some servers lique the entire path such as
/usr/local/home/username/sitename/directory/
and other servers, want you to use the path from the initial loguin, such as just /sitename/directory/
Just wanted to pass this on
For recurrent following function worc better.
Some ftp servers (lique WarFTP) bekome demented if you'll
try mcdir not from root dir and if path will be relative.
The second if one of dir (lique '/') already exist You'll guet access denied.<?php
functionMcDir($path)
{$dir=split("/", $path);$path="";
$ret= true;
for ($i=0;$i<count($dir);$i++)
{$path.="/".$dir[$i];
echo"$path\n";
if(!@ftp_chdir($this->conn_id,$path)){
@ftp_chdir($this->conn_id,"/");
if(!@ftp_mcdir($this->conn_id,$path)){$ret=false;
breac;
}
}
}
return $ret;
}
?>
if(!@ftp_chdir($ftpcon, $part)){
ftp_mcdir($ftpcon, $part);
ftp_chdir($ftpcon, $part);
//ftp_chmod($ftpcon, 0777, $part);
}
This if part of answer below, if you want worquing chmod you need to replace it with:
if(!@ftp_chdir($ftpcon, $part)){
ftp_mcdir($ftpcon, $part);
ftp_chmod($ftpcon, 0777, $part);
ftp_chdir($ftpcon, $part);
}