PHP mysqli: set_local_infile_handler() function
mysqli_set_local_infile_handler function / mysqli::set_local_infile_handler
The mysqli_set_local_infile_handler function / mysqli::set_local_infile_handler — Set callback function for LOAD DATA LOCAL INFILE command.
Syntax:
Object oriented style
bool mysqli::set_local_infile_handler ( mysqli $link , callable $read_func )
Procedural style
bool mysqli_set_local_infile_handler ( mysqli $link , callable $read_func )
Parameter:
| Name | Description | Required/Optional | |
|---|---|---|---|
| link | A link identifier returned by mysqli_connect() or mysqli_init() | Required for procedural style only and Optional for Object oriented style | |
| read_func | A callback function or object method taking the following parameters: | Required | |
| stream | A PHP stream associated with the SQL commands INFILE | ||
| &buffer | A string buffer to store the rewritten input into. | ||
| buflen | The maximum number of characters to be stored in the buffer | ||
| &errormsg | If an error occurs you can store an error message in here | ||
Usage: Procedural style
mysqli_set_local_infile_handler(connection);
Parameter:
| Name | Description | Required/Optional | 
|---|---|---|
| connection | Specifies the MySQL connection to use | Required | 
Return value:
Returns TRUE on success or FALSE on failure.
Version: PHP 5, PHP 7
Example of object oriented style:
<?php
$db = mysqli_init();
  $db->real_connect("localhost","user1","datasoft123","hr");
  function callme($stream, &$buffer, $buflen, &$errmsg)
  {
    $buffer = fgets($stream);
    echo $buffer;
    // convert to upper case and replace "," delimiter with [TAB]
    $buffer = strtoupper(str_replace(",", "\t", $buffer));
    return strlen($buffer);
  }
  echo "Input:\n";
  $db->set_local_infile_handler("callme");
  $db->query("LOAD DATA LOCAL INFILE 'input.txt' INTO TABLE t1");
  $db->set_local_infile_default();
  $res = $db->query("SELECT * FROM t1");
  echo "\nResult:\n";
  while ($row = $res->fetch_assoc()) {
    echo join(",", $row)."\n";
  }
?>
Example of the Procedural style
<?php
$link = mysqli_connect('localhost', 'my_user', 'my_password', 'test');
/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
printf("Initial character set: %s\n", mysqli_character_set_name($link));
/* change character set to utf8 */
if (!mysqli_set_charset($link, "utf8")) {
    printf("Error loading character set utf8: %s\n", mysqli_error($link));
    exit();
} else {
    printf("Current character set: %s\n", mysqli_character_set_name($link));
}
mysqli_close($link);
?>
Output:
Input: 23,foo 42,bar Output: 23,FOO 42,BAR
Example:
<?php
 
  $con = mysqli_init();
  $con->real_connect("localhost","user1","datasoft123","hr");
  function Calling($stream, &$buffer, $buflen, &$errmsg)
  {
    $buffer = fgets($stream);
    echo $buffer;
    // convert to upper case and replace "," delimiter with [TAB]
    $buffer = strtoupper(str_replace(",", "\t", $buffer));
    return strlen($buffer);
  }
  echo "Input:\n";
  $con->set_local_infile_handler("Calling");
  $con->query("LOAD DATA LOCAL INFILE 'MyInput.txt' INTO TABLE table1");
  $con->set_local_infile_default();
  $res = $con->query("SELECT * FROM table1");
  echo "\nResult:\n";
  while ($row = $res->fetch_assoc()) {
    echo join(",", $row)."\n";
  }
?>
Output:
Input:
See also
Previous: set_local_infile_default
Next:  sqlstate
