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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2003
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    reading txt files

    im sure this is extremely simple and im not seeing it.

    what i have is a txt file with a number in it, say:

    count.txt
    --------------
    5
    --------------

    now i want to use this number as a variable in my php file. i know this next example is incorrect but its the easiest way of expressing what i want:

    items.php
    --------------
    <?php
    $total = include 'count.txt'; // so i want $total to equal 5
    $newtotal = $total+1; // so i want $newtotal to equal 6
    $fp = fopen('./count.txt' , 'w');
    fwrite($fp, $newtotal);
    fclose($fp);
    ?>
    --------------

    so basically, i think my problem is that Im not sure exactly how to use the number in count.txt as a variable in my items.php. Any help would be greatly appreciated! Thanks
    Last edited by acorath; 12-24-2003 at 12:52 AM.

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,081
    Thanks
    11
    Thanked 99 Times in 97 Posts
    IF your php version is >=4.3.0

    PHP Code:
    <?
    $total 
    trimfile_get_contents'count.txt' ) ) ;
    ?>

    else (and there are several ways this is just one of them )

    PHP Code:
    <?
    $file 
    file'count.txt' ) ;
    $total trim$file[0] ) ;
    ?>
    the trim() is simply to get rid of the newline that may or may not exist.

    ... since you are already using fopen etc , you shoud also checkout the manual on fread() , fgets() & friends
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    New to the CF scene
    Join Date
    Dec 2003
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thanks

    thanks a ton! I'll be sure to read up on those


  •  

    Posting Permissions

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