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 5 of 5
  1. #1
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post

    Problems with fopen

    It keeps reporting problems with line 5 in this code..

    PHP Code:
    <?php

    $file 
    fopen("num.txt""r+")

    fputs($file"$file++");

    fclose($file);

    echo (
    $file);

    ?>
    Whats the big problem?

  • #2
    Regular Coder
    Join Date
    Mar 2004
    Posts
    115
    Thanks
    0
    Thanked 0 Times in 0 Posts
    missing a semi colon on line 3

  • #3
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    Yeah.. finally spotted that heh...

    But now I am getting weird results..

    When I run it I get a print out of this..

    PHP Code:
    Resource id #2 
    The original file has the number "100" in it & this is what I get by adding +1 to it????

    All I am trying to do is increnement the files number by 1 & then get that new number in a variable.

  • #4
    Regular Coder
    Join Date
    Mar 2004
    Posts
    115
    Thanks
    0
    Thanked 0 Times in 0 Posts
    fopen links $file to a resourceid which is the pointer to the files internal position you would then need to use fread() to read the content using the $file resouceid and a loop

    a better way to read the contents of the file in to a string is below

    PHP Code:
    <?php
    $file
    ="num.txt";
    $content file_get_contents($file); 
    $content++;
    $resourceid fopen($file"w");
    fputs($resourceid$content); 
    fclose($resourceid); 
    echo (
    $content);

  • #5
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    Ahh.. I see what you mean! :-)

    Thanks for the code, works good!


  •  

    Posting Permissions

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