How can PHP scripts be used to change folder permissions for successful file uploads?

When uploading files to a server using PHP, it is important to ensure that the destination folder has the correct permissions set in order for the upload to be successful. This can be done using PHP scripts to change the folder permissions before uploading the files.

// Set the path to the folder where the files will be uploaded
$uploadFolder = 'uploads/';

// Set the permissions for the folder (e.g. 755 for read/write/execute for owner and read/execute for group and others)
$permissions = 0755;

// Change the folder permissions
if (!file_exists($uploadFolder)) {
    mkdir($uploadFolder, $permissions, true);
} else {
    chmod($uploadFolder, $permissions);
}