How can PHP be used to assign ownership of a directory to Apache for better security?

Assigning ownership of a directory to Apache can improve security by allowing the web server to have the necessary permissions to access and modify files within that directory. This can help prevent unauthorized access and potential security vulnerabilities.

<?php
// Set the directory path
$directory = '/path/to/directory/';

// Assign ownership of the directory to Apache
chown($directory, 'apache');