What are the risks associated with inserting binary files directly into a database using PHP?

Inserting binary files directly into a database using PHP can pose security risks such as SQL injection attacks and potential performance issues due to the large size of binary data. To mitigate these risks, it is recommended to store binary files in a separate directory on the server and only store the file path or reference in the database.

// Example of storing binary file in a separate directory and saving the file path in the database
$binaryData = file_get_contents('path/to/binary/file.jpg');
$filePath = 'uploads/file.jpg';

// Save file to server
file_put_contents($filePath, $binaryData);

// Insert file path into database
$query = "INSERT INTO files (file_path) VALUES ('$filePath')";
// Execute query using PDO or mysqli