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 11 of 11
  1. #1
    Regular Coder Rappa's Avatar
    Join Date
    Nov 2005
    Location
    LI
    Posts
    356
    Thanks
    21
    Thanked 9 Times in 9 Posts

    Help with writing to a file

    so i have a form with a name and email field, and the action points to a php file with this code:

    PHP Code:
    <?php
    $name 
    $_POST["name"];

    $file "012608.txt";

    $values "$name\n";

    $fp fopen($file"w") or die("Couldn't open $file for writing!");
    $numBytes fwrite($fp$values) or die("Couldn't write values to file!");

    fclose($fp);
    echo 
    "You have successfully signed up for this class.";

    ?>

    Now when i hit submit it gives the echo with no errors, but when i open the text file in the browser, its blank, any ideas?
    Last edited by Rappa; 01-17-2008 at 05:09 AM.

  • #2
    Regular Coder Rappa's Avatar
    Join Date
    Nov 2005
    Location
    LI
    Posts
    356
    Thanks
    21
    Thanked 9 Times in 9 Posts
    lol ok well fixed that problem, forgot the method="post" in the form.

    But new problem, it works, but when i do it again, it over writes, how can i have it write names one after another without overwriting the previous?

  • #3
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Rappa View Post
    lol ok well fixed that problem, forgot the method="post" in the form.

    But new problem, it works, but when i do it again, it over writes, how can i have it write names one after another without overwriting the previous?
    , use "w+" instead of "w" in fopen,

    best regards

  • Users who have thanked oesxyl for this post:

    Rappa (01-17-2008)

  • #4
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    w and w+ both 'truncate the file to zero length', you need to use a or a+

  • Users who have thanked Mwnciau for this post:

    Rappa (01-17-2008)

  • #5
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Mwnciau View Post
    w and w+ both 'truncate the file to zero length', you need to use a or a+
    you are right, + is for reading, sorry for the wrong answer.

    best regards

  • Users who have thanked oesxyl for this post:

    Rappa (01-17-2008)

  • #6
    Regular Coder Rappa's Avatar
    Join Date
    Nov 2005
    Location
    LI
    Posts
    356
    Thanks
    21
    Thanked 9 Times in 9 Posts
    Thanks guys it worked. You rox

  • #7
    Regular Coder Rappa's Avatar
    Join Date
    Nov 2005
    Location
    LI
    Posts
    356
    Thanks
    21
    Thanked 9 Times in 9 Posts
    just out of curiosity, whats the difference between a and a+

  • #8
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    a is just like w, its for writing only, both a+ and w+ can read as well as write, it's explained on the manual page for fopen

  • #9
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    These are all the different writing modes.
    http://www.php.net/manual/en/function.fopen.php

    mode Description
    'r' Open for reading only; place the file pointer at the beginning of the file.
    'r+' Open for reading and writing; place the file pointer at the beginning of the file.
    'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
    'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
    'a' Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
    'a+' Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
    'x' Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call.
    'x+' Create and open for reading and writing; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • Users who have thanked StupidRalph for this post:

    Rappa (01-17-2008)

  • #10
    Regular Coder Rappa's Avatar
    Join Date
    Nov 2005
    Location
    LI
    Posts
    356
    Thanks
    21
    Thanked 9 Times in 9 Posts
    Thank you kindly!

  • #11
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Rappa View Post
    just out of curiosity, whats the difference between a and a+
    a+ open add reading mode to a.
    In fact + means both mode, writing and reading, thats why could be w+, r+, a+.

    EDIT: Sorry, for repeating the same thing, but when I start edit this, was no answer, :-)

    best regards
    Last edited by oesxyl; 01-17-2008 at 06:21 AM.


  •  

    Posting Permissions

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