📜  PHP | ftp_exec()函数

📅  最后修改于: 2022-05-13 01:56:51.303000             🧑  作者: Mango

PHP | ftp_exec()函数

ftp_exec()函数是PHP中的一个内置函数,用于在 FTP 服务器上执行命令。

句法:

ftp_exec( $ftp_connection, $command )

参数:该函数接受上面提到的两个参数,如下所述:

  • $ftp_connection:必填参数。它指定用于执行 FTP 命令或功能的现有 FTP 连接。
  • $command:必填参数。指定在连接成功的 FTP 服务器上执行的命令。

返回值:成功时返回True,失败时返回False。

笔记:

  • 此函数适用于PHP 4.0.3 及更新版本。
  • 以下示例无法在在线 IDE 上运行。所以尝试使用正确的 ftp 服务器名称、用户和密码在一些PHP托管服务器或 localhost 中运行。

例子:

PHP
 test.txt";
  
// 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 "
logged in successfully!";                   // ftp_exec() executes the command         if (ftp_exec($ftp_connection, $command)) {             echo "
".$command." has been successfully executed.";         }         else {             echo "
Error while executing the command .";         }     }     else {         echo "
login failed!";     }           // Closing  connection     if(ftp_close($ftp_connection)) {         echo "
Connection closed Successfully!";     } } ?>


输出:

successfully connected to the ftp server!
logged in successfully!
ls-al > test.txt has been successfully executed.
Connection closed Successfully!

参考: https://www. PHP.net/manual/en/函数.ftp-exec。 PHP