How to change content on a file?

Discussion in 'PHP' started by killaklown, Aug 20, 2006.

  1. #1
    How would I be able to change the content on a file? (like have config.php and have a page which you can change the settings to it).
     
    killaklown, Aug 20, 2006 IP
  2. danielbruzual

    danielbruzual Active Member

    Messages:
    906
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    70
    #2
    if you have the settings in a file you have to use fopen() and fwrite() to open the file and write the contents. It would be much easier if you could save the config settings in a database, then you would just have to connect to it and voila (use sql queries to change the data).
     
    danielbruzual, Aug 20, 2006 IP
  3. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #3
    Ok, thanks, ill use the database then.
     
    killaklown, Aug 20, 2006 IP
  4. yellowindian

    yellowindian Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    could u post an example with fopen() and fwrite()

    take 2 settings

    sitename

    siteurl

    thanks waiting for reply
     
    yellowindian, Aug 20, 2006 IP
  5. danielbruzual

    danielbruzual Active Member

    Messages:
    906
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    70
    #5
    Let's say you have a file uploading site, and you have a configuration file which contains the file size limit (I will call this file 'size.txt'). I want to be able to change this limit from the admin area of my site. We will need a file which will display the current settings and allow you to change them (call it 'admin.php').

    the file that contains the file size limit (in Kilobytes)
    
    2048000
    
    Code (size.txt):
    the page from where you can see the current setting and modify it
    
    <?
    If(isset($_POST['changesize'])){
    	$fop =  fopen('size.txt', 'w');
    	$size = $_POST['size'];	
    	If(fwrite($fop,$size)){
    		echo "Max file size changed successfully.";
    	}else{
    		echo "Max file size could not be changed.";
    }
    }
    ?>
    <?
    	$fop =  fopen('size.txt', 'r');
    	$size = fread($fop, '999');
    	fclose($fop);
    ?>
    <h3>Set Max File Size (Bytes)</h3>
    Current file size limit is set at <? echo $size; ?>
    <br />
    <form method="post" action="">
    <input type="text" name="size" value="<?echo $size;?>" />(1Mb = 1024000Bytes)
    <br /><input type="submit" name="changesize">
    </form>
    
    Code (admin.php):
     
    danielbruzual, Aug 20, 2006 IP
  6. yellowindian

    yellowindian Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    this is for 1 setting

    can u right a code for more than 2 settings

    can u plz explain

    sorry for disturbing you
     
    yellowindian, Aug 20, 2006 IP
  7. danielbruzual

    danielbruzual Active Member

    Messages:
    906
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    70
    #7
    You store the settings in a text file (.txt) and you want to be able to change them with php.

    the file that contains the settings
    
    I Love Bananas Dot Com|http://www.ilovebananas.com
    
    Code (settings.txt):
    the page from where you can see the current settings and modify them
    
    <?
    //First we check if the form has been submitted
    If(isset($_POST['changesettings'])){
    
    //we open the file for writing, it if does not exist php will attempt to create it
    	$fop =  fopen('settings.txt', 'w');
    
    //retrieve the data from the form
    	$sitename = $_POST['sitename'];	
    	$siteurl = $_POST['siteurl'];
    
    //prepare the data for writing into the file
    	$newcontent = $sitename."|".$siteurl;
    
    //write the data to the file, user will be notified if it was successful
    	If(fwrite($fop,$newcontent)){
    		echo "Settings changed successfully.";
    	}else{
    		echo "Settings could not be changed.";
    }
    
    //close the file
    	fclose($fop);
    }
    ?>
    <?
    //open the file
    	$fop =  fopen('settings.txt', 'r');
    
    //read the first 999 bytes of the file
    	$content = fread($fop, '999');
    
    //close the file
    	fclose($fop);
    
    //seperate the contents of the file into an array 
    	$content = explode("|", $content);
    ?>
    <h3>Change Settings</h3>
    Current site name is <? echo $content[0]; ?><br />
    Current url is <? echo $content[1]; ?><br />
    <form method="post" action="">
    New Site Name<input type="text" name="sitename" value="<?echo $content[0];?>" /><br />
    New Site URL<input type="text" name="siteurl" value="<?echo $content[1];?>" /><br />
    <input type="submit" name="changesettings">
    </form>
    
    PHP:
    There you go, it might have a flaw or two but it should be easily fixed.

    a few tutorials for you:

    http://www.softwareprojects.org/php-files-15.htm
    http://www.free2code.net/plugins/articles/read.php?id=84

    Daniel
     
    danielbruzual, Aug 20, 2006 IP
  8. yellowindian

    yellowindian Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    suppose there are 3 lines

    and i want the script to echo the second line

    how do i do it


    can u also teach me how do i use this settings thing with radio buttons

    like on and off

    and also how to use it with dropdown

    plz explain with codes

    ur 1 st code taught me a lot

    thanks for dat
     
    yellowindian, Aug 21, 2006 IP