当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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 
  
// 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"; 
  
// Command to be executed on FTP server 
$command="ls-al > 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 "<br>logged in successfully!"; 
          
        // ftp_exec() executes the command  
        if (ftp_exec($ftp_connection, $command)) { 
            echo "<br>".$command." has been successfully executed."; 
        } 
        else { 
            echo "<br>Error while executing the command ."; 
        } 
    } 
    else { 
        echo "<br>login failed!"; 
    } 
      
    // Closeing  connection 
    if(ftp_close($ftp_connection)) { 
        echo "<br>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/function.ftp-exec.php



相关用法


注:本文由纯净天空筛选整理自gekcho大神的英文原创作品 PHP | ftp_exec() function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。