Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    Dec 2009
    Posts
    24
    Thanks
    2
    Thanked 0 Times in 0 Posts

    edit flatfile can somebody try this code

    hi, im trying out this code to get the content from a flatfile and it works but
    after submitting it dont change the content

    can somebody take a look at this code or try it together with a flat filet o see if it works
    / thanks Lisa

    <?php
    if ($changefile) {
    $slash = stripslashes($_POST['filetest']);
    $filetochange = "http://yoursite.com/file.txt ";
    $filetochangeOpen = fopen($filetochange,"w") or die ("Error editing.");
    fputs($filetochangeOpen,$slash);
    fclose($filetochangeOpen) or die ("Error Closing File!");
    }
    ?>
    <form method=post action="">

    <textarea rows="40" cols="60" name="filetest">
    <?
    // Implode CSS
    $filetochange = "http://yoursite.com/file.txt";
    print (implode("",file($filetochange)));
    ?>
    </textarea><br />

    <br />
    <input type="submit" value="Change File" name="changefile">
    </form>
    Last edited by danielandlisa; 03-19-2010 at 03:28 PM. Reason: type wrong

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    238
    Thanks
    3
    Thanked 37 Times in 37 Posts
    You would be able to write to file if it was on the local file system and had permissions for writing. You would be unable to write to a file if you open it over HTTP. The corresponding wrapper does not support writing.
    Last edited by SKDevelopment; 03-19-2010 at 03:39 PM.

  • #3
    New Coder
    Join Date
    Dec 2009
    Posts
    24
    Thanks
    2
    Thanked 0 Times in 0 Posts
    ok but how do they mean this code should be used then?
    i got this for another forum and they say this code can be used to update
    flatfiles through php
    Last edited by danielandlisa; 03-19-2010 at 03:47 PM. Reason: type wrong

  • #4
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Quote Originally Posted by danielandlisa View Post
    ok but how do they mean this code should be used then?
    i got this for another forum and they say this code can be used to update
    flatfiles through php
    Answer:
    Quote Originally Posted by SKDevelopment View Post
    You would be able to write to file if it was on the local file system and had permissions for writing.
    You're not accessing the local file system when you're using http.
    Are you a Help Vampire?

  • #5
    New Coder
    Join Date
    Dec 2009
    Posts
    24
    Thanks
    2
    Thanked 0 Times in 0 Posts
    so what would i need to do make it writable

  • #6
    Regular Coder
    Join Date
    Mar 2006
    Posts
    238
    Thanks
    3
    Thanked 37 Times in 37 Posts
    Are you trying to write to a file located at the same server where the script is run ? Then use local file system path instead of URL. If the file has write permissions (for the user under which PHP works), you would be able to write to it.

    Under *nix it could be something like this:
    PHP Code:
    $filetochangeOpen fopen("/home/myfolder/file.txt""w"); 
    Under Windows it could be something like this:
    PHP Code:
    $filetochangeOpen fopen("C:\\myfolder\\file.txt""w"); 
    Last edited by SKDevelopment; 03-19-2010 at 04:01 PM.

  • #7
    New Coder
    Join Date
    Dec 2009
    Posts
    24
    Thanks
    2
    Thanked 0 Times in 0 Posts
    thanks for the replies, does it matter if i use the www path or the local path when i use the local path i get failed to open stream but when i use the www path i get the content from the text file. the problem is that it wont write to the file . the txt file is chmod to 777

  • #8
    Regular Coder
    Join Date
    Mar 2006
    Posts
    238
    Thanks
    3
    Thanked 37 Times in 37 Posts
    Try to check that your local path to file is correct. You could check it e.g. with file_exists().

  • #9
    New Coder
    Join Date
    Dec 2009
    Posts
    24
    Thanks
    2
    Thanked 0 Times in 0 Posts
    thanks i will try it, maybe the search path is the problem

    could you confirm if the code works for you , if you got time that is

  • #10
    Regular Coder
    Join Date
    Mar 2006
    Posts
    238
    Thanks
    3
    Thanked 37 Times in 37 Posts
    If I change the line
    PHP Code:
    if ($changefile) { 
    to
    PHP Code:
    if ($_POST['changefile']) { 
    and change the path in fopen() to local file system path, then saving works.

    I would recommend to change that condition in case you have register_globals off (as I do).

  • Users who have thanked SKDevelopment for this post:

    danielandlisa (03-19-2010)

  • #11
    New Coder
    Join Date
    Dec 2009
    Posts
    24
    Thanks
    2
    Thanked 0 Times in 0 Posts
    ok thanks alot
    hi just to clarify its the absolute path i should use?
    Last edited by danielandlisa; 03-19-2010 at 05:11 PM. Reason: type wrong

  • #12
    New Coder
    Join Date
    Dec 2009
    Posts
    24
    Thanks
    2
    Thanked 0 Times in 0 Posts
    thanks alot skd, found out the absolute path now it works good

  • #13
    Regular Coder
    Join Date
    Mar 2006
    Posts
    238
    Thanks
    3
    Thanked 37 Times in 37 Posts
    You are always welcome.

    I tested with relative. In the production environment I would probably use absolute ...


  •  

    Posting Permissions

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