简体   繁体   中英

PHP get_file_contents with FTP server file

I making something and i need a bit of help. First i using FTP informations to connect to server and get some file something like this:

$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);      
}

}

The file 'serverlist.ini' looks like this :

; 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

With this PHP code bellow i get something like this:

[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

And now i need to get on other file just to get something like this from this serverlist.ini via get_file_contents : From serverlist.ini file to get just address and port in format 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

Sorry for my bad english i think u guys understand what i need :)

Verify that this path actually exists:

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

Meaning if you login to FTP and navigate to /cstrike/addons/amxmodx/configs/ , you will see the serverlist.ini file.

If you are able to get the file, the next thing you need to do is parse the INI file. This can be achieved via parse_ini_string()

Example:

$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++;
    }
}

Which will yield:

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

See full example/test here: http://codepad.viper-7.com/K2hB46

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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