What potential issues can arise when file permissions are not properly set in PHP scripts for file uploads?

When file permissions are not properly set in PHP scripts for file uploads, it can lead to security vulnerabilities such as allowing unauthorized users to upload malicious files or overwrite existing files. To solve this issue, it is important to ensure that the directory where files are uploaded has the correct permissions set to prevent unauthorized access.

// Set the correct permissions for the upload directory
$uploadDir = 'uploads/';
if (!file_exists($uploadDir)) {
    mkdir($uploadDir, 0777, true);
}
chmod($uploadDir, 0755);