繁体   English   中英

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM