PHP get_file_contents 和 FTP 服务器文件

PHP get_file_contents with FTP server file

我在做东西,需要一些帮助。 首先,我使用 FTP 信息连接到服务器并获取类似这样的文件:

$ftp_ip = $info['ftp_ip'];
$ftp_user = $info['ftp_user'];
$ftp_password = $info['ftp_password'];

$filename = "ftp://$ftp_user:$ftp_password@$ftp_ip/cstrike/addons/amxmodx/configs/serverlist.ini";  

$file_headers = file_get_contents($filename);
if($file_headers == ""){
 echo " error ";        
} else {

$lines = file($filename);

foreach ($lines as $line_num => $line) {
   if (!preg_match("/^(\;|\s)/",$line)) echo nl2br($line);      
}

}

文件 'serverlist.ini' 如下所示:

; Menu configuration file
; File location: $moddir/addons/amxmodx/configs/configs.ini
; To use with Commands Menu plugin

; NOTE: By default in all settings the access level is set to "u".
;       However you can change that, to limit the access to some settings.

; Commands Menu:
; < description > < command > < flags > < access level >
; "a" - execute from server console
; "b" - execute from admin console
; "c" - execute on all clients
; "d" - back to menu when executed

[Extreme ProGaming Fun]
address=77.105.36.100
port=27028
noauto=0
nodisplay=0


[Extreme ProGaming Knife]
address=77.105.36.102
port=27010
noauto=0
nodisplay=0


[Extreme ProGaming DeatMatch]
address=77.105.36.103
port=27026
noauto=0
nodisplay=0

使用下面的 PHP 代码,我得到如下信息:

[Extreme ProGaming Fun]
address=77.105.36.100
port=27028
noauto=0
nodisplay=0
[Extreme ProGaming Knife]
address=77.105.36.102
port=27010
noauto=0
nodisplay=0
[Extreme ProGaming DeatMatch]
address=77.105.36.103
port=27026
noauto=0
nodisplay=0

现在我需要获取其他文件,以便通过 get_file_contents 从 serverlist.ini 中获取类似的内容: 从 serverlist.ini 文件获取格式为 adress:port

的地址和端口
1. 77.105.36.100:27028  // Extreme ProGaming Fun
2. 77.105.36.102:27010  // Extreme ProGaming Knife
3. 77.105.36.103:27026  // Extreme ProGaming DeatMatch

抱歉我的英语不好我想你们明白我需要什么:)

验证此路径确实存在:

ftp://$ftp_user:$ftp_password@$ftp_ip/cstrike/addons/amxmodx/configs/serverlist.ini

意思是如果您登录 FTP 并导航至 /cstrike/addons/amxmodx/configs/,您将看到 serverlist.ini 文件。

如果您能够获取该文件,接下来您需要做的就是解析 INI 文件。这可以通过 parse_ini_string()

来实现

示例:

$ftp_ip = $info['ftp_ip'];
$ftp_user = $info['ftp_user'];
$ftp_password = $info['ftp_password'];

$filename = "ftp://$ftp_user:$ftp_password@$ftp_ip/cstrike/addons/amxmodx/configs/serverlist.ini";  

$contents = file_get_contents($filename);
if(false === $contents){
 echo " error ";        
} else {
    $ini = parse_ini_string($contents, true); // use true in second parameter to indicate this INI has blocks
    $i = 1;
    foreach($ini as $server => $config) {
        echo "$i. {$config['address']}:{$config['port']} // $server\n";
        $i++;
    }
}

将产生:

1. 77.105.36.100:27028 // Extreme ProGaming Fun
2. 77.105.36.102:27010 // Extreme ProGaming Knife
3. 77.105.36.103:27026 // Extreme ProGaming DeatMatch

在此处查看完整 example/test:http://codepad.viper-7.com/K2hB46