What are the best practices for managing file permissions in PHP to ensure smooth file management operations via FTP?

When managing file permissions in PHP to ensure smooth file management operations via FTP, it is important to set appropriate permissions for files and directories to allow the FTP user to read, write, and execute as needed. This can be achieved by using the chmod() function in PHP to set the permissions accordingly.

// Set permissions for a file
$file = 'example.txt';
$permissions = 0644; // Read and write for owner, read for others
if (file_exists($file)) {
    chmod($file, $permissions);
}

// Set permissions for a directory
$directory = 'uploads';
$permissions = 0755; // Read, write, and execute for owner, read and execute for others
if (is_dir($directory)) {
    chmod($directory, $permissions);
}