Search code examples
phpfopenfgets

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);
}

Solution

  • $handle = @fopen("http://www.example.com/", "r");
    
    $buffer = '';
    if ($handle) {
        while (!feof($handle)) {
          $buffer .= fgetss($handle, 5000);
        }
    
        fclose($handle);
    }