SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot 8Observer8's Avatar
    Join Date
    Jul 2013
    Location
    Russia
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP. Writing to the file.

    Hi,

    I write to the file:

    Code:
    <?php
        $handle = fopen('names.txt', 'w');
        fwrite($handle, 'Ivan'."\n");
        fwrite($handle, 'Billy'."\n");
        fclose($handle);
    ?>
    I expect to see:
    Ivan
    Billy
    But I see:
    Code:
    IvanBilly
    P.S. When I copied to this forum from file I saw:
    Ivan
    Billy
    Thank you.

  2. #2
    SitePoint Zealot 8Observer8's Avatar
    Join Date
    Jul 2013
    Location
    Russia
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't understand. This code works correctly:

    Code:
    <?php
    $filename = 'names.txt';
    
    if (isset($_POST['name'])) {
        $name = $_POST['name'];
        if (!empty($name)) {
            $handle = fopen($filename, 'a');
            fwrite($handle, $name."\n");
            fclose($handle);
            
            echo 'Current names in file: ';
            
            $count = 0;
            $readin = file($filename);
            $readin_count = count($readin);
            foreach ($readin as $fname) {
                echo trim($fname);
                if ($count < $readin_count-1) {
                    echo ', ';
                }
                $count++;
            }
        }
    }
    ?>
    
    <form action="file.php" method="POST">
        <input type="text" name="name" />
        <br />
        <input type="submit" name="submit" value="Append" />
    </form>
    But in the file I see:
    IvanMaxDanPeterIvan
    When I open the file names.txt in the browser I see:

    Ivan
    Max
    Dan
    Peter
    Ivan

  3. #3
    SitePoint Evangelist bronze trophy
    Join Date
    Feb 2013
    Posts
    507
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    Try
    Code:
    \r\n
    This might also be an option but \r\n will probably do the job in your case.
    Code:
    PHP_EOL

  4. #4
    SitePoint Zealot 8Observer8's Avatar
    Join Date
    Jul 2013
    Location
    Russia
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much

  5. #5
    SitePoint Member
    Join Date
    Sep 2013
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <?php
    $handle = fopen('names.txt', 'w');
    fwrite($handle, "Ivan\n");
    fwrite($handle, "Billy\n");
    fclose($handle);
    ?>
    can you try this code.

  6. #6
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,813
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by robertboblalas View Post
    <?php
    $handle = fopen('names.txt', 'w');
    fwrite($handle, "Ivan\n");
    fwrite($handle, "Billy\n");
    fclose($handle);
    ?>
    can you try this code.
    That will produce the same scenario that he previously had. The issue because the EOL (end of line) character used. Which varies by operating system. Windows, Mac, and Linux all use different EOL. It is usually safe to use \r\n, and you can use PHP_EOL if you do not need to transfer your file between different operating systems (otherwise the PHP_EOL will be different on each system, so it might not read back the same).
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  7. #7
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,011
    Mentioned
    56 Post(s)
    Tagged
    0 Thread(s)
    Guys, for most file I/O scenarios file_get_contents and file_put_contents will do the job. fopen and the related functions used to be the only way to do this sort of things, these days they really are just for situations where the size of the file makes loading it into memory impractical.


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
  •