PHP | ftp_chdir() function
Last Updated :
28 Jul, 2021
The ftp_chdir() function is an inbuilt function in PHP which is used to change the current directory on the FTP server.
Syntax:
ftp_chdir( $ftp_connection, $directory )
Parameter: This function accepts two parameters as mentioned above and described below:
- $ftp_connection: It is required parameter. It specifies the already existing FTP connection to use for execution of FTP commands or functions.
- $directory: It is required parameter. It specifies the path in remote server to which current directory to be changed.
Return Value: It returns True on success or False on failure.
Note:
- This function is available for PHP 4.0.0 and newer version.
- The following examples cannot be run on online IDE. So try to run in some PHP hosting server or localhost with a proper ftp server name, user and password.
- Make sure you have permission to change directory and access to the directory.
Example:
php
<?php
$ftp_server = "localhost" ;
$ftp_username = "username" ;
$ftp_userpass = "password" ;
$ftp_connection = ftp_connect( $ftp_server )
or die ( "Could not connect to $ftp_server" );
if ( $ftp_connection ) {
echo "successfully connected to the ftp server!" ;
$login = ftp_login( $ftp_connection , $ftp_username , $ftp_userpass );
if ( $login ) {
echo "<br>logged in successfully!" ;
if (ftp_chdir( $ftp_connection , "htdocs" )) {
echo "<br>Current directory successfully changed to htdocs." ;
}
else {
echo "<br>Error while changing current directory." ;
}
}
else {
echo "<br>login failed!" ;
}
if (ftp_close( $ftp_connection )) {
echo "<br>Connection closed Successfully!" ;
}
}
?>
|
Output:
successfully connected to the ftp server!
logged in successfully!
Current directory successfully changed to htdocs.
Connection closed Successfully!
Reference: https://www.php.net/manual/en/function.ftp-chdir.php
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...