Not signed in (Sign In)

SkillShare - A place to discuss Web Standards and Web Design topics

Categories

Vanilla 1.1.9 is a product of Lussumo. More Information: Documentation, Community Support.

    • CommentAuthorspoo
    • CommentTimeDec 14th 2006 edited
     permalink
    I made a PHP script that creates a text file with text in it that you type into a form. The filename is locked in the form, so it adds new data to the beginning of the document using rewind(). However, using rewind(), for some reason, things are not showing up at the beginning of the document like it's supposed to, instead, it's adding things to the end. I'm also getting the errors:


    Warning: fwrite(): supplied argument is not a valid stream resource in /nfsn/content/lemmings/htdocs/testblog/action.php on line 15

    Warning: fclose(): supplied argument is not a valid stream resource in /nfsn/content/lemmings/htdocs/testblog/action.php on line 17

    Any help? Here's the php.


    <?
    $filename = $_POST[$filename];
    $theText = $_POST[$theText];

    $theText = stripslashes($theText);

    $data = fopen($filename, "a");

    fwrite($data,$theText . "
    ");

    fclose($data);

    echo "File created or updated";
    ?>
    • CommentAuthordhayes
    • CommentTimeDec 14th 2006 edited
     permalink

    $filename = 'yourfilename.txt';
    $new_text = 'the new text';
    if(!file_exists($filename)){
    touch($filename);
    }
    $file_content = implode('',file($filename));
    $content = $new_text."\n".$file_content;
    $handle = fopen($filename, 'wb');
    if(fwrite($handle, $content)===FALSE){
    echo 'failed to write to '.$filename;
    } else {
    echo 'wrote "'.$new_text.'" to '.$filename;
    }
    fclose($handle);


    just a thought.. it's best to define the file path/name in the code and not allow it to be specified by the post. also, the "$newtext" , if it comes from the post, you'll want to trim it and add the new line character in yourself.

    $newtext = trim($_POST['newtext']);


    good luck..

    PS: I'm pretty sure there's an fopen switch that will let you prepend to the top of the file when writing..which would save a little bit of overhead.. no time dig into it at the moment, but you might want to.
Add your comments
    Username Password
  • Format comments as (Help)