跟之前”以 PHP 實現 POP3 authentication“差不多
這次打算借用 FTP 既有的認證機制!!
藉由判別 response message 來達到認證的目的!!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function CheckFTP($server, $id, $passwd, $port = 21) | |
{ | |
//若任一欄位為空白則無效 | |
if (empty($server) || empty($id) || empty($passwd)) | |
return false; | |
//連結FTP Server | |
$fs = fsockopen ($server, $port, &$errno, &$errstr, 5); | |
//檢查是否連線 | |
if (!$fs) | |
return false; | |
//connected.. | |
$msg = fgets($fs,256); | |
//step 1. 傳送帳號 | |
fputs($fs, "USER $id\r\n"); | |
$msg = fgets($fs,256); | |
if (strpos($msg,"331")===false) | |
return false; | |
//step 2. 傳送密碼 | |
fputs($fs, "PASS $passwd\r\n"); | |
$msg = fgets($fs,256); | |
if (strpos($msg,"230")===false) | |
return false; | |
//step 3.通過認證 QUIT | |
fputs($fs, "QUIT \r\n"); | |
fclose($fs); | |
return true; | |
} | |
?> |
Basic FTP Command:
- USER 帳號
- PASS 密碼
- DELE 刪除
- QUIT 離線