Jump to content

Edit line values problem in PHP


ChrisMartino

Recommended Posts

Well i have a function to edit find the value in a file using SSH2 and PHP, Here's what i have:

 

My problem is that it won't edit the values for some reason, I've checked and all the directory's that it is fetching are correct but no file editing takes place?.

public function CheckClientConfig($serverid)
	{
		if($this->IsValidGameServer($serverid))
		{
			$ClearID = mysql_real_escape_string($serverid);
			$ObtainDetails = mysql_query("SELECT * FROM gameservers WHERE ServerID = '".$ClearID."'");

			$Fetch = mysql_fetch_array($ObtainDetails);

			$SelectServer = mysql_query("SELECT * FROM servers WHERE ServerName = '".$Fetch[OnServer]."'");

			$FetchServer = mysql_fetch_array($SelectServer);

			$SelectPackage = mysql_query("SELECT * FROM serverpackages WHERE PackageNAME = '".$Fetch[Game]."'");

			$PackageRow = mysql_fetch_array($SelectPackage);

			$ssh = new Net_SSH2($FetchServer['IP']);

     			if (!$ssh->login($FetchServer['RootUsername'], $FetchServer['RootPassword'])) 
			{
         			exit('There was a error Re-Starting the server, Please re-try or contact support.');
     			}		

			$ReadConfig = $ssh->exec("sudo -u root cat /home{$Fetch['Directory']}/{$PackageRow['ConfigFile']}");
			        
			$config_file = explode("\n",trim(str_replace("\r",'',$ReadConfig)));
			$config_done = array();
			$config = '';

			foreach($config_file as $key => $line)
			{
				$line = trim($line);
				$ex = explode(' ',$line);
				if(strlen($ex[0]) != 0)
				{
					if(isset($ex[1])) $config_done[strtolower($ex[0])] = (string)$ex[1];
					else $config_done[strtolower($ex[0])] = true;
					if(strcasecmp($ex[0],'maxplayers') == 0)
					{
						$slots = (int) $ex[1];
						if($slots > $Fetch['Slots'] or $slots == 0)
						{
							$config .= 'maxplayers '.$Fetch['Slots']."\n";
						}
						else
						{
							$config .= 'maxplayers '.$slots."\n";
						}
					}
					elseif(strcasecmp($ex[0],'port') == 0)
					{
						$config .= 'port '.$Fetch['Port']."\n";
					}
					else
					{
						$config .= $line."\n";
					}
				}
			}

			if(!isset($config_done['maxplayers'])) $config .= 'maxplayers '.$Fetch['Slots']."\n";
			elseif($config_done['maxplayers'] === true) $config .= 'maxplayers '.$Fetch['Slots']."\n";

			$ssh->exec("echo '/home{$Fetch[Directory]}/{$PackageRow[ConfigFile]}' > ".$config);

		}
	}

Link to comment
https://forums.phpfreaks.com/topic/201108-edit-line-values-problem-in-php/
Share on other sites

you should use echo like this

echo 'string' > /path/to/file

 

for example

	

$ssh->exec("echo '$config' > /home{$Fetch[Directory]}/{$PackageRow[ConfigFile]}");

 

Also, the code posted in your linux thread should work fine

 

This way still doesn't seem to work though? :[

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.