带有FTP服务器文件的PHP get_file_contents

时间:2015-07-15 15:27:00

标签: php ftp file-get-contents

我做了些什么,我需要一些帮助。 首先,我使用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

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

1 个答案:

答案 0 :(得分:1)

验证此路径是否确实存在:

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

在此处查看完整示例/测试:http://codepad.viper-7.com/K2hB46