JennyL
JennyL

Reputation: 33

Php Save fopen Result To A String

How can I get the "$buffer" value into a string, and use it outside the fopen and fclose functions? Thanks.

$handle = @fopen("http://www.example.com/", "r");

if ($handle) {
    while (!feof($handle)) {
        $buffer = fgetss($handle, 5000);

      echo $buffer ;
    }

    fclose($handle);
}

Upvotes: 3

Views: 7875

Answers (5)

Ale
Ale

Reputation: 443

Try file_get_contents() :

$buffer = file_get_contents("/my/file.txt");

Upvotes: 7

mahadeb
mahadeb

Reputation: 676

$handle = @fopen("http://www.example.com/", "r");
$buffers = array();

    if ($handle) {
        while (!feof($handle)) {
            $buffers[] = fgetss($handle, 5000);

        }

        fclose($handle);
    }


print_r ( $buffers );

Upvotes: 0

Tomas
Tomas

Reputation: 5143

The easiest way is to use file_get_contents:

$buffer = file_get_contents("http://www.exemple.com");

Upvotes: 1

Kabindra Yahoo
Kabindra Yahoo

Reputation: 1

$buffer is itself a string. instead of printing it using echo just concatenate it there and print it or use it with all together after the loop ends.

$buffer = '';
if ($handle) {
    while (!feof($handle)) {
      $buffer .= fgetss($handle, 5000);
    }

    fclose($handle);
}

//print the whole stuff:
echo $buffer;

And if you want to get all the stuffs only no other processing try using:

file_get_contents

Upvotes: 0

Emil Vikström
Emil Vikström

Reputation: 91942

$handle = @fopen("http://www.example.com/", "r");

$buffer = '';
if ($handle) {
    while (!feof($handle)) {
      $buffer .= fgetss($handle, 5000);
    }

    fclose($handle);
}

Upvotes: 3

Related Questions