簡體   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