What are best practices for handling file permissions in PHP upload scripts to avoid issues with FTP access?

When handling file uploads in PHP scripts, it is important to set appropriate file permissions to avoid potential security risks and ensure proper access control. To avoid issues with FTP access, it is recommended to set the file permissions to restrict access to only the necessary users or groups. This can be achieved by using the chmod function in PHP to set the desired permissions on the uploaded files.

// Set appropriate file permissions for uploaded files
$uploadDir = 'uploads/';
$uploadedFile = $uploadDir . basename($_FILES['file']['name']);

// Check if file was uploaded successfully
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadedFile)) {
    // Set file permissions to restrict access
    chmod($uploadedFile, 0644);
    echo "File uploaded successfully.";
} else {
    echo "Error uploading file.";
}