PHP Function Reference

PHP ob_start() Function



The PHP ob_start() function turns on the output buffering. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer. A callback function can be passed to the function to process the contents of the buffer before it is flushed from the buffer. Flags can be used to permit or restrict what the buffer is able to do.

The contents of this internal buffer may be accessed using ob_get_contents() function. To output what is stored in the internal buffer, ob_end_flush() can be used. On the other hand, ob_end_clean() function can be used to silently discard the buffer contents.

Output buffers are stackable, that is, ob_start() can be called while another ob_start() is active. Just make sure that the ob_end_flush() function is called the appropriate number of times. If multiple output callback functions are active, output will be filtered sequentially in the nesting order.

If output buffering is still active when the script ends, PHP outputs the contents automatically.

Syntax

ob_start(callback, chunk_size, flags)

Parameters

callback Required. Specify the FTP connection to use.
chunk_size Optional. Specify the FTP connection to use.
flags Optional. Specify the FTP connection to use.

Return Value

Returns true on success or false on failure.

Example:

The example below shows the usage of ob_start() function.

<?php
//FTP server to use
$ftp_server = "ftp.example.com";

//username for the FTP Connection
$ftp_user = "user";
  
//password for the user
$ftp_pass = "user";
   
//set up a connection or die 
$ftp = ftp_connect($ftp_server)
    or die("Could not connect to $ftp_server");
   
if($ftp) {
  echo "Successfully connected to $ftp_server!\n";
 
  //trying to login
  if(@ftp_login($ftp, $ftp_user, $ftp_pass)) {
    echo "Connected as $ftp_user@$ftp_server\n";

    $dir = "images/";

    //trying to delete the $dir directory
    if (ob_start($ftp, $dir)) {
      echo "Successfully deleted $dir directory\n";
    } else {
      echo "Error while deleting $dir\n";
    }
  } else {
    echo "Couldn't connect as $ftp_user\n";
  }
 
  //close the connection
  if(ftp_close($ftp)) {
    echo "Connection closed successfully!\n"; 
  } 
}
?>

The output of the above code will be:

Successfully connected to ftp.example.com!
Connected as user@ftp.example.com
Successfully deleted images/ directory
Connection closed successfully!

❮ PHP Output Control Reference

5