What potential issues can arise when manipulating file extensions in PHP, and how can they be avoided?

One potential issue that can arise when manipulating file extensions in PHP is that it can lead to security vulnerabilities if not handled properly. To avoid this, always validate and sanitize user input to prevent malicious file uploads or injections. Additionally, make sure to use secure file handling functions and avoid executing files with unknown extensions.

// Example of validating and sanitizing file extension
$allowed_extensions = ['jpg', 'png', 'gif'];
$filename = $_FILES['file']['name'];
$extension = pathinfo($filename, PATHINFO_EXTENSION);

if (!in_array($extension, $allowed_extensions)) {
    die("Invalid file extension. Please upload a JPG, PNG, or GIF file.");
}

// Further file handling code goes here