www.webdeveloper.com
Results 1 to 9 of 9

Thread: write to .inc file

  1. #1
    Join Date
    Feb 2005
    Location
    Tauranga
    Posts
    2,062

    Question write to .inc file

    Hi, what i want to be able to do is open a plain text .inc file in a iframe or textarea and make plain text changes to the words in there then be able to save the file back on the server?

    Does anybody know how to do this or if it is posiable?



    Thanks

  2. #2
    Join Date
    Nov 2004
    Location
    Walla Walla, WA
    Posts
    89
    Yes, it is quite possible. To read the text file into a textarea would probably be best.

    To read the existing file into the textarea:

    PHP Code:
    <?php

    $fp
    =fopen("something.inc""r");

    while (!
    feof($fp)){
     
    $line=fgets($fp);
     print 
    "$line";
    }
    //end while

    fclose($fp);

    ?>
    To write the .inc file (assuming you named the textarea "contents"):

    PHP Code:
    <?php

    $fp
    =fopen("something.inc""w");
    fputs($fp$_REQUEST['contents']);

    if (
    fclose($fp)){
     print 
    "<h2>File successfully saved</h2>\n";
    }else{
     print 
    "<h2><font style=\"color:red;\">There was a problem saving the file</font></h2>\n";
    }

    ?>
    Make your changes in the first page, and submit the form containing the textarea to the page that writes to the file. Note that using "w" in the file pointer will erase what was written and write whatever you're passing to the file. This is what you want in this case, I think, but if you only want to append to the end of the file for some reason, DON'T use "w". See http://us2.php.net/manual/en/function.fopen.php for more on writing to files.
    -Ben Sauve
    www.bensauve.com

  3. #3
    Join Date
    Feb 2005
    Location
    Tauranga
    Posts
    2,062
    Thanks but it did not seam to work

    i have
    HTML Code:
    <form action="<?php print($_SERVER['PHP_SELF']); ?>" method="post">
     <textarea rows="6" coll="40" name="comments">
    <?php 
    
    $fp=fopen("textqtc.php", "w"); 
    fputs($fp, $_REQUEST['comments']); 
    
    if (fclose($fp)){ 
     print "<h2>File successfully saved</h2>\n"; 
    }else{ 
     print "<h2><font style=\"color:red;\">There was a problem saving the file</font></h2>\n"; 
    } 
    
    ?>
    </textarea>
     <input type="submit" value="submit">
    </form>

    it does not load any page or text?

  4. #4
    Join Date
    Nov 2004
    Location
    Walla Walla, WA
    Posts
    89
    That's because you're using the code that writes to the file. Use the first block of code to read from the file into the textarea.
    -Ben Sauve
    www.bensauve.com

  5. #5
    Join Date
    Feb 2005
    Location
    Tauranga
    Posts
    2,062
    it doesnt work am i not reading what you are say correctly?

    the link is

    http://showmycar.co.nz/slweb/clients...qtc1/index.php

    i have the read code inside a textarea in a form, when it loads to write.php which is just the plain write php code, the text box reads empty

  6. #6
    Join Date
    Nov 2004
    Location
    Walla Walla, WA
    Posts
    89
    Um, I imagine the first time you actually wrote nothing over thge top of your existing file. Not much you can do about that now except restore it from a backup. Opening a file pointer with the "w" creates a new file or overwrites an existing one. You might check to see if the file you're reading from is blank.
    -Ben Sauve
    www.bensauve.com

  7. #7
    Join Date
    Feb 2005
    Location
    Tauranga
    Posts
    2,062
    go to the link and view source, its not right , the page only loads to the start of the text bax and in the source is not good

  8. #8
    Join Date
    Nov 2004
    Location
    Walla Walla, WA
    Posts
    89
    Kinda looks like it's too busy writing "<b>Warning</b>: Wrong parameter count for fgets()..." over and over to finish up, huh? Let me try something, see what happens with that same code reading from a blank file... gimme a minute.
    -Ben Sauve
    www.bensauve.com

  9. #9
    Join Date
    Nov 2004
    Location
    Walla Walla, WA
    Posts
    89
    Ok, this is a complete test page I just made, copying and pasting the code above into it. This simple code will produce an error if the file you're trying to include doesn't exist, so you might add an "if(is_file('filename.inc')){..." or something for this, but-

    Ok, create a new text file, rename it "blank.php" and upload it to the same place as this test page. Change the CHMOD to 777 for blank.php, and then load this page (call it whatever you want, I called it test.php):

    PHP Code:
    <html>
    <body>

    <?php
     
    if(empty($_GET['action'])){
    ?>

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>?action=write" method="post">
    <textarea name="contents" rows="10" cols="60">
    <?php 

    $fp
    =fopen("blank.php""r"); 

    while (!
    feof($fp)){ 
    $line=fgets($fp); 
    print 
    "$line"
    }
    //end while 

    fclose($fp); 

    ?> 
    </textarea><br/>
    <input type="submit" value="Submit" />
    </form>

    <?php

     
    }else{

    $fp=fopen("blank.php""w"); 
    fputs($fp$_REQUEST['contents']); 

    if (
    fclose($fp)){ 
    print 
    "<h2>File successfully saved</h2>\n"
    }else{ 
    print 
    "<h2><font style=\"color:red;\">There was a problem saving the file</font></h2>\n"


    print 
    "<input type=\"button\" value=\"Go Back\" onClick=\"window.location.href='".$_SERVER['PHP_SELF']."';\" />";

    }
    //end if action

    ?>
    See if you can do something like this to your page. See the code in action at http://gaston9x19.modcentral.us/test/test.php. Let me know what happens.
    -Ben Sauve
    www.bensauve.com

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles