SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    PEACE WILL WIN abalfazl's Avatar
    Join Date
    Feb 2005
    Location
    Beyond the seas there is a town
    Posts
    711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation A file,but different output,When using different functions:readfile,fread

    Hello my firends



    PHP Code:
    <?php
    /* Open a test file for writing */
    $fp fopen("test.txt""w+");
    /* Apply the ROT13 filter to the
     * write filter chain, but not the
     * read filter chain */
    stream_filter_append($fp"string.rot13",
    STREAM_FILTER_WRITE);
    /* Write a simple string to the file
     * it will be ROT13 transformed at the end of the
    stream operation
     * way out */
    fwrite($fp"This is a test\n"); // string data is
    //first written, then ROT13 tranformed and lastly
    //written to file*/
    /* Back up to the beginning of the file */
    rewind($fp);
    $contents=fread($fp,512);
    readfile("test.txt");

    fclose($fp);
    echo 
    $contents;
    ?>

    The output is:

    Guvf vf n grfg
    This is a test


    When we use readfile(),It prints:

    Guvf vf n grfg

    But,When we use fread(),It prints:

    This is a test

    Why?
    I shall build a boat,I shall cast it in the water,
    I shall sail away from this strange earth,
    Where no one awaken the heroes in the wood of love

  2. #2
    SitePoint Addict yjanse's Avatar
    Join Date
    Apr 2004
    Location
    The Netherlands
    Posts
    240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Doesn't it overwrite? With mode 'w+' the filepointer is put at the beginning of the file. Maybe it will work better if you use 'a+', then the filepointer will be at the end of the file, in append-mode.
    Yves Janse:
    Android42.net - PHP5 Bookcase project - Developers Blog
    | PHP | MySQL | CSS | XML | XHTML | AJAX | Patterns |


Bookmarks

Posting Permissions

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