#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2000
    Posts
    0
    Rep Power
    0
    Ok,

    What i am trying to do is have a form which allows users to submit a news story to my sites archives. They have to enter the title, their name and the story itself. When they click submit i want my PHP script to create a new text file (using fopen()???) and the data to be written to it.

    My problem is that when I use fopen() i can't get it to create a new file. I read the manual and i think it said to use:

    fopen("file.txt", "a+"); to create a new file if file.txt doesnt exist.

    But i cant get it to work. Can anyone tell me the correct syntax???

    And also, when I do this:

    $stuff = readfile("File.txt");
    echo $stuff

    why do i get the number of characters in the file appearing at the end?

    Thanks
    Jamie Beatson
  2. #2
  3. No Profile Picture
    Robert_J_Sherman
    Guest
    Devshed Newbie (0 - 499 posts)
    to create the new file use:

    fopen($path_to_file.$filename,"w+"):



    ------------------
    SnR Graphics,
    Low Cost Hosting and Web Development.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Posts
    4
    Rep Power
    0
    Also make sure your permissions are set correctly.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    Melbourne, Victoria, Australia
    Posts
    104
    Rep Power
    14
    Regarding your readfile() problem...

    Firstly, read the manual page for readfile() (here.)

    Then once you've read that you'll see the bit that says that readfile() returns the number of bytes in the file that is being read. I think what you're problem is is that you don't realise that when you go:

    $stuff = readfile(bla bla bla);

    it's not actually reading the file into $stuff, it's outputting the file.

    then, because you have the line that goes:

    echo $stuff;

    It's just tacking the number of bytes in the file onto the end.

    Hope that helps,

    Josh
  8. #5
  9. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    6
    Rep Power
    0
    This should do the trick for reading out your .txt file
    $filename = "file.txt";
    $stuff = file($filename);

    for($i=0 ; $i < count($stuff) ; $i++)
    {
    echo $stuff[$i];
    }

    ------------------
    Until next time, I'll Remain!!
    Quote the Dokus:"NeverMore!"
  10. #6
  11. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Brussels, Belgium
    Posts
    14,642
    Rep Power
    4476
    Even better is this: (sorry, had to do it Dokus)

    $file=file("file.txt");
    $string=implode("",$file);
    echo $string;


    ---John Holmes...
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2000
    Posts
    0
    Rep Power
    0
    Thanks for your help guys, i had figured out Dukus' method myself yesterday, but thanks anyway!

    Also, Sepodati - WHat does the implode function do exactly? I have seen implode() mentioned in another post, but i dont know what it does (havent checked the manual yet)

    Jamie
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    Melbourne, Victoria, Australia
    Posts
    104
    Rep Power
    14
    awe come one... what's wrong with readfile?

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>readfile('myfile.txt');[/code]

    hehehe

    Josh
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2000
    Posts
    7
    Rep Power
    0
    Since this is a contest of sorts (just kidding) then here is one way to do it...... i would not do it this way..... (i personally liek the readfile() way) but here are my two cents::

    <?
    passthru("more /location/of/File.txt");
    ?>

    hehe ..... well it diplays the contents....
    ccbcreg
  18. #10
  19. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Brussels, Belgium
    Posts
    14,642
    Rep Power
    4476
    www.php.net/implode

    readfile() is the easiest, if you don't need the file actually assigned to a variable.

    ---John Holmes...

IMN logo majestic logo threadwatch logo seochat tools logo