What are the best practices for handling file uploads in PHP forms and storing file paths in a database?

When handling file uploads in PHP forms, it's important to use the $_FILES superglobal to access the uploaded file data and move the file to a secure directory on the server. To store file paths in a database, you can simply insert the file path into a database table along with any other relevant information. Make sure to sanitize user input to prevent SQL injection attacks.

// Handle file upload
if(isset($_FILES['file'])){
    $file_name = $_FILES['file']['name'];
    $file_tmp = $_FILES['file']['tmp_name'];
    $file_destination = 'uploads/' . $file_name;

    if(move_uploaded_file($file_tmp, $file_destination)){
        // File uploaded successfully
    } else {
        // Error uploading file
    }
}

// Store file path in database
$file_path = 'uploads/' . $file_name;
// Perform sanitization on $file_path before inserting into database

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