How can one ensure that the directory where files are being uploaded has the correct permissions in PHP?

When uploading files in PHP, it is important to ensure that the directory where the files are being uploaded has the correct permissions set to allow for file uploads. This can be done by setting the appropriate permissions on the directory using the chmod() function in PHP. By setting the correct permissions, you can ensure that files can be uploaded successfully without any permission issues.

// Set the correct permissions on the directory where files will be uploaded
$directory = 'uploads/';
$permissions = 0777; // Set the permissions as needed

if (!file_exists($directory)) {
    mkdir($directory, $permissions, true);
} else {
    chmod($directory, $permissions);
}