How can the "LOAD DATA LOCAL INFILE" command be modified to allow access to external files in PHP?

When using the "LOAD DATA LOCAL INFILE" command in PHP to load data from a local file into a MySQL database, the default settings may prevent access to external files for security reasons. To allow access to external files, you can modify the MySQL configuration file to include the "local-infile=1" option under the [mysqld] section. This change will enable the use of the "LOAD DATA LOCAL INFILE" command with external files in PHP.

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Enable access to external files
$conn->query("SET GLOBAL local_infile = 1");

// Use the LOAD DATA LOCAL INFILE command to load data from an external file
$sql = "LOAD DATA LOCAL INFILE '/path/to/external/file.csv' INTO TABLE tablename FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' IGNORE 1 LINES";
if ($conn->query($sql) === TRUE) {
    echo "Data loaded successfully";
} else {
    echo "Error loading data: " . $conn->error;
}

$conn->close();
?>