What are the advantages of using a database for storing file upload information in PHP?

When storing file upload information in PHP, using a database has several advantages. It allows for easy organization and retrieval of uploaded files, provides better security by storing file paths instead of actual files on the server, enables scalability for handling large amounts of files, and facilitates data management and backup processes.

// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "uploads";

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

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

// Store file upload information in the database
$filename = $_FILES['file']['name'];
$filetype = $_FILES['file']['type'];
$filesize = $_FILES['file']['size'];
$file_path = "uploads/" . $filename;

$sql = "INSERT INTO files (filename, filetype, filesize, file_path) VALUES ('$filename', '$filetype', '$filesize', '$file_path')";

if ($conn->query($sql) === TRUE) {
    echo "File uploaded successfully.";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();