PHP | ftp_mkdir() function
Last Updated :
26 Aug, 2021
The ftp_mkdir() function is an inbuilt function in PHP which is used to create a new directory on the ftp server. Once the directory is created cannot be created again. Creating a directory that already exists will produce error.
Syntax:
string ftp_mkdir( $ftp_connection, $directory_name )
Parameter: This function accepts two parameters as mentioned above and described below:
- $ftp_connection: It is the required parameter and used to specify the ftp connection on which directory to be created.
- $directory_name:It is the required parameter and used to specify the name of the directory to be created.
If child directory is to be created in an existing or non-existing directory then the $directory_name parameter to be set in the format "(parent directory name)/(child directory name)/(child of child directory name)/..." so on. For example, create a directory named as childdirectory inside testdirectory then $directory_name = "testdirectory/childdirectory";
Return Value: It returns the name of the directory that created on success, False on failure.
Note:
- This function is available for PHP 4.0.0 and newer version.
- The following example cannot be run on online IDE. So try to run in some PHP hosting server or localhost with proper ftp server name and correct username and password.
Example 1:
php
<?php
// Connecting to ftp server
// Use ftp server address
$fserver = "ftp.gfg.org";
// Use ftp username
$fuser="username";
// Use ftp password
$fpass="password";
// Connect to the ftp server
$f_conn = ftp_connect($fserver) or
die("Could not connect to $fserver");
// Authenticating to ftp server
$login = ftp_login($f_conn, $fuser, $fpass);
// Directory name which is to be created
$dir = "testdirectory";
// Creating directory
if (ftp_mkdir($f_conn, $dir)) {
// Execute if directory created successfully
echo " $dir Successfully created";
}
else {
// Execute if fails to create directory
echo "Error while creating $dir";
}
// Closing ftp connection
ftp_close($f_conn);
?>
Output:
testdirectory Successfully created
Example 2: In case of child directory to be created then everything is same as before except $dir i.e. directory name.
php
<?php
//Connecting to ftp server
// Use ftp server address
$fserver = "ftp.exampleserver.com";
// Use ftp username
$fuser="username";
// Use ftp password
$fpass="password";
// Connecting to ftp server
$f_conn = ftp_connect($fserver) or
die("Could not connect to $fserver");
// Authenticating to ftp server
$login = ftp_login($f_conn, $fuser, $fpass);
// Directory name which is to be created
$dir = "testdirectory/childdirectory";
// Creating directory
if (ftp_mkdir($f_conn, $dir)) {
// Execute if directory created successfully
echo " $dir Successfully created";
}
else {
// Execute if fails to create directory
echo "Error while creating $dir";
}
// Closing ftp connection
ftp_close($f_conn);
?>
Output:
testdirectory/childdirectory Successfully created
Note: If directory name already exist then it produce error.
Reference: http://php.net/manual/en/function.ftp-mkdir.php
Similar Reads
PHP | mkdir( ) Function The mkdir() creates a new directory with the specified pathname. The path and mode are sent as parameters to the mkdir() function and it returns TRUE on success or FALSE on failure. The mode parameter in mkdir() function is ignored on Windows platforms. Syntax: mkdir(path, mode, recursive, context)
2 min read
PHP | ftp_nlist() function The ftp_nlist() function is an inbuilt function in PHP which is used to get the list of all the filename and sub-directory in a specific directory on FTP server. Syntax:Â ftp_nlist( $ftp_connection, $directory ); Parameters: This function accepts two parameters as mentioned above and described below
3 min read
PHP | ftp_login() function The ftp_login() function is an inbuilt function in PHP which is used to login to an established FTP connection. Syntax: ftp_login( $ftp_connection, $ftp_username, $ftp_userpass ); Parameter: This function accepts three parameters as mentioned above and described below: $ftp_connection: It is require
2 min read
PHP | ftp_pwd() function The ftp_pwd() function is an inbuilt function in PHP which returns the current directory name of the specified FTP connection. This function was introduced in PHP 4. Syntax: string ftp_pwd( $ftp_conn ) Parameter: This function accepts single parameter $ftp_conn which is used to specify the used FTP
2 min read
PHP | ftp_put() function The ftp_put() function is an inbuilt function in PHP which is used to upload files to FTP server.Syntax:Â Â ftp_put( $ftp_connection, $remote_file_path, $local_file_path, $mode, $start_position ); Parameter: This function accepts five parameters as mentioned above and described below:Â Â $ftp_connecti
3 min read