How can directory and file permissions be properly configured in PHP to ensure functionality without compromising security?

Directory and file permissions in PHP can be properly configured by setting the appropriate permissions for directories and files to ensure functionality without compromising security. Directories should typically have permissions set to 755, allowing read, write, and execute permissions for the owner, and read and execute permissions for group and others. Files should have permissions set to 644, allowing read and write permissions for the owner, and read permissions for group and others. This ensures that the PHP scripts can access and modify files as needed while preventing unauthorized access.

// Set directory permissions to 755
chmod("/path/to/directory", 0755);

// Set file permissions to 644
chmod("/path/to/file", 0644);