Editing a file w/ a php form?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bkbelew
Forum Newbie
Posts: 3
Joined: Wed Jan 19, 2005 8:02 pm

Editing a file w/ a php form?

Post by bkbelew »

Hello,

Im new to php, I can only do the basics. includes, a little bit of smarty templates etc. Im trying to figure out something, that is probably very simple, but I just cant figure it out. I need a form, to edit a file, save the file, and execute a batch file or windows 2003 console command.

Code: Select all

<!-- Generated by FireDaemon OEM v1.7 GA (Build 1913) -->

 

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>

<!DOCTYPE Service SYSTEM "FireDaemon.dtd">

<Service>

 <Program>

  <Name>hlds server</Name>

  <DisplayName>hlds</DisplayName>

  <Description>Hlds dedicated server</Description>

  <WorkingDir>D:\hlds\</WorkingDir>

  <Executable>D:\hlds\hlds.exe</Executable>

  <Parameters>+ip 67.19.224.36 -port 27015 -game dod -heapsize 131072 -maxplayers 20 +map dod_avalanche -console -autoupdate</Parameters>

  <Delay>3000</Delay>

  <ConsoleApp>false</ConsoleApp>

  <ForceReplace>true</ForceReplace>

 </Program>

 <Options>

  <AffinityMask>0</AffinityMask>

  <Priority>0</Priority>

  <AppendLogs>true</AppendLogs>

  <EventLogging>true</EventLogging>

  <InteractWithDesktop>true</InteractWithDesktop>

  <PreLaunchDelay>0</PreLaunchDelay>

  <StartUpMode>1</StartUpMode>

  <UponExit>1</UponExit>

  <UponFlap>0</UponFlap>

  <FlapCount>0</FlapCount>

  <ShutdownDelay>5000</ShutdownDelay>

  <ShowWindow>2</ShowWindow>

  <JobType>0</JobType>

 </Options>

 <Debug>

  <DebugEnabled>true</DebugEnabled>

  <DebugLocation>d:\hlds\debug\</DebugLocation>

 </Debug>

 <SMF>

  <SMFEnabled>true</SMFEnabled>

  <SMFFrequency>5000</SMFFrequency>

 </SMF>

 <Scheduling>

   <StartTime>00:00:00</StartTime>

   <EndTime>00:00:00</EndTime>

   <RunDays>127</RunDays>

   <MonthFrom>0</MonthFrom>

   <MonthTo>0</MonthTo>

   <MonthDay>0</MonthDay>

   <RestartFreq>0</RestartFreq>

   <RestartDelay>0</RestartDelay>

 </Scheduling>

</Service>
The form would need to edit the

Code: Select all

<Parameters>+ip 67.19.224.36 -port 27015 -game dod -heapsize 131072 -maxplayers 20 +map dod_avalanche -console -autoupdate</Parameters>
Portion of the xml file, not the entire <parameters> string though, just the -game dod part and the +map part. Maybe with a drop down menu w/ a selection of mod type w/ default maps?.

After that information is changed, the file would need to be saved, then the command or a batch file like the below would need to be executed

Code: Select all

net stop hlds
C:\program files\firedaemon\firedaemon -i filename.xml
How hard would this be to do? If easy, can someone please help me?

Brandon
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

instead of editing the existing file, I'd suggest having a template file so php can load that seperately, and create a new version of the xml file.

You can use file_get_contents() to quickly and easily load in the entire file as a string.. and str_replace() to find and replace all instances of the "unique" string you want to use as the placeholder.

fopen() will open and create the new version, fwrite() will write the new contents of the string returned from str_replace() back out.

pretty easy.
bkbelew
Forum Newbie
Posts: 3
Joined: Wed Jan 19, 2005 8:02 pm

Post by bkbelew »

Problem with using a template is, then id have to have a php form make all edit all the contents, since im going to be using it for multiple services.

Could i do this?

Code: Select all

<?php

file_get_contents(C:\program files\firedaemon\hlds.xml) 

str_replace(-game dod)
str_replace(+map de_dust)

fwrite(C:\program files\firedaemon\hlds.xml)

?>
If so, how do I specify what im replacing -game dod and +map with?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

try reading through the documentation to the functions I mentioned. here's a starting point: http://php.net/str_replace
bkbelew
Forum Newbie
Posts: 3
Joined: Wed Jan 19, 2005 8:02 pm

Post by bkbelew »

I still dont get it :)

I get the concept, and how it works. But how would I make the replacement value a value from a form?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

retrieving the submitted information is done through the $_GET or $_POST superglobals... http://us2.php.net/manual/en/language.v ... efined.php

you may also want to search here for (potentially) "+form +valid*" sans quotes. Additionally, have a look through the tutorials section for threads about forms and things..
Post Reply