[英]How to send FTP commands without listing directory, or transferring files with curl?
我正在嘗試向 ProFTPD 服務器發送一些標准命令,curl 總是發送LIST
命令,而我的命令結果被LIST
響應覆蓋。
curl_setopt($curl, CURLOPT_URL, "ftp://domain.xyz:21");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_QUOTE, array('PWD'));
$result=curl_exec($curl);
日志文件包含:
> PWD
< 257 "/" is the current directory
> PASV
* Connect data stream passively
< 227 Entering Passive Mode (xxx,xxx,xxx,xxx,xxx,xxx).
* Hostname was NOT found in DNS cache
* Trying xxx.xxx.xxx.xxx...
* Connecting to xxx.xxx.xxx.xxx (xxx.xxx.xxx.xxx) port 39794
* Connected to xyz (xxx.xxx.xxx.xxx) port 21 (#0)
> TYPE A
< 200 Type set to A
> LIST
< 150 Opening ASCII mode data connection for file list
我想得到“257”/“是當前目錄”這一行。
更新:
有一個選項CURLOPT_NOBODY
,它停用LIST
命令,但我仍然無法獲得PWD
命令的響應,即使使用CURLOPT_CUSTOMREQUEST
。
我不能使用 PHP 的 FTP 命令,因為 Windows 上的 PHP 沒有ftp_ssl_connect
功能。 是否有其他具有 TLS 支持和上傳/下載進度處理程序的 FTP 庫?
我不認為 curl 是為這樣的任務設計的。
話雖如此,您可以通過啟用日志記錄和解析來自日志的響應來破解它。
function curl_ftp_command($curl, $command)
{
// Create a temporary file for the log
$tmpfile = tmpfile();
// Make curl run our command before the actual operation, ...
curl_setopt($curl, CURLOPT_QUOTE, array($command));
// ... but do not do any operation at all
curl_setopt($curl, CURLOPT_NOBODY, 1);
// Enable logging ...
curl_setopt($curl, CURLOPT_VERBOSE, true);
// ... to the temporary file
curl_setopt($curl, CURLOPT_STDERR, $tmpfile);
$result = curl_exec($curl);
if ($result)
{
// Read the output
fseek($tmpfile, 0);
$output = stream_get_contents($tmpfile);
// Find the request and its response in the output
// Note that in some some cases (SYST command for example),
// there can be a curl comment entry (*) between the request entry (>) and
// the response entry (<)
$pattern = "/> ".preg_quote($command)."\r?\n(?:\* [^\r\n]+\r?\n)*< (\d+ [^\r\n]*)\r?\n/i";
if (!preg_match($pattern, $output, $matches))
{
trigger_error("Cannot find response to $command in curl log");
$result = false;
}
else
{
$result = $matches[1];
}
}
// Remove the temporary file
fclose($tmpfile);
return $result;
}
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "ftp://domain.xyz:21");
echo curl_ftp_command($curl, "PWD");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.