Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2005
    Location
    Norway
    Posts
    391

    make a files content a variable

    Hey,

    How do I make a files content a variable?
    Like if a file named output.txt contains the following line:
    sometext blablabla 0123456789 more test
    how would I use PHP to put that into a variable?

    I know maybe fread or so but I am not sure how to use it!:\

    Can anyone please show me some code on how to do this or give me some start help?
    All tips apprecciated, thanks!!

  2. #2
    Join Date
    Mar 2003
    Location
    South Bend, Indiana, USA
    Posts
    320
    Try using:
    PHP Code:
    <?
    $wp
    =fopen("output.txt","r");
    // here you receive content of page
    while(!feof($wp)) {
     
    $content .= fread($wp,4096); // load the contents into a variable
    }
    fclose($wp); // close output.txt
    echo $content// display the contents of output.txt which is stored in $content
    ?>
    Daniel
    WLScripting.com - php scripts and tutorials

  3. #3
    Join Date
    Jun 2004
    Location
    Bay Area -USA
    Posts
    1,738
    Even easier way to do it, if you're running the php 4.3.x or newer

    PHP Code:
    $contents file_get_contents("output.txt"); 
    The php site recommends doing it this way because it's utilizes the OS (if supported) and makes reading the file faster
    <<< Please see Forum Guidelines for signature setup. >>>

  4. #4
    Join Date
    Feb 2005
    Location
    Norway
    Posts
    391
    Thanks alot!

  5. #5
    Join Date
    Mar 2003
    Location
    South Bend, Indiana, USA
    Posts
    320
    Nice, I have not come across that function before. I guess I can say thanks as well.

    I guess I need to go hit up php.net even more to check out all the functions. (Better take a couple days )
    Daniel
    WLScripting.com - php scripts and tutorials

  6. #6
    Join Date
    Jun 2004
    Location
    Bay Area -USA
    Posts
    1,738
    hehe no problem.
    <<< Please see Forum Guidelines for signature setup. >>>

Posting Permissions

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