函式名:ftp_exec()
適用版本:PHP 4, PHP 5, PHP 7
用法:ftp_exec(resource $ftp_stream, string $command) : bool
描述:ftp_exec() 函式用於在 FTP 伺服器上執行指定的命令。
引數:
- $ftp_stream:FTP 連線資源,透過 ftp_connect() 或 ftp_ssl_connect() 函式獲得。
- $command:要在 FTP 伺服器上執行的命令。
返回值:如果命令執行成功,則返回 true,否則返回 false。
示例:
// 連線到 FTP 伺服器
$ftp_conn = ftp_connect('ftp.example.com');
// 登入到 FTP 伺服器
$login = ftp_login($ftp_conn, 'username', 'password');
// 執行命令
$command = 'ls -al';
$result = ftp_exec($ftp_conn, $command);
if ($result === false) {
echo "命令執行失敗";
} else {
echo "命令執行成功";
}
// 關閉 FTP 連線
ftp_close($ftp_conn);
上述示例中,我們首先使用 ftp_connect() 函式連線到 FTP 伺服器,然後使用 ftp_login() 函式登入到伺服器。接著,我們使用 ftp_exec() 函式執行了一個命令,即列出當前目錄下的所有檔案和資料夾。最後,我們根據返回值判斷命令是否執行成功,並關閉 FTP 連線。
請注意,ftp_exec() 函式執行的命令需要符合 FTP 伺服器支援的語法和許可權。具體的命令格式和引數,請參考你所使用的 FTP 伺服器的文件或相關資源。