PHP如何使用ftp_chdir()函数(代码示例)

ftp_chdir()函数是PHP中的内置函数, 用于更改FTP服务器上的当前目录。
语法如下:

ftp_chdir( $ftp_connection, $directory )

【PHP如何使用ftp_chdir()函数(代码示例)】参数:此函数接受上述和以下所述的两个参数:
  • $ ftp_connection:它是必填参数。它指定用于执行FTP命令或功能的现有FTP连接。
  • $目录:必填参数。它指定远程服务器中要将当前目录更改到的路径。
返回值:成功返回True, 失败返回False。
注意:
  • 此功能可用于PHP 4.0.0和更高版本。
  • 以下示例无法在在线IDE上运行。因此, 请尝试使用适当的ftp服务器名称, 用户名和密码在某些PHP托管服务器或localhost中运行。
  • 确保你具有更改目录和访问目录的权限。
例子:
< ?php// Connect to FTP server// Use a correct ftp server $ftp_server = "localhost" ; // Use correct ftp username $ftp_username = "username" ; // Use correct ftp password corresponding // to the ftp username $ftp_userpass = "password" ; // Establishing ftp connection $ftp_connection = ftp_connect( $ftp_server ) or die ( "Could not connect to $ftp_server" ); if ( $ftp_connection ) { echo "successfully connected to the ftp server!" ; // Logging in to established connection with // ftp username password $login = ftp_login( $ftp_connection , $ftp_username , $ftp_userpass ); if ( $login ) {// Checking whether logged in successfully or not echo "< br> logged in successfully!" ; // ftp_chdir() changing current directory to "htdocs" // remember, you must have folder that will use inside // current directory of ftp server. // Here htdocs folder exists in ftp server inside // base or root directory 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!" ; }// Closeingconnection if (ftp_close( $ftp_connection )) { echo "< br> Connection closed Successfully!" ; } } ?>

输出如下:
successfully connected to the ftp server! logged in successfully! Current directory successfully changed to htdocs. Connection closed Successfully!

参考: https://www.php.net/manual/en/function.ftp-chdir.php

    推荐阅读