How can PHP developers ensure proper file naming conventions when uploading files to specific folders?

To ensure proper file naming conventions when uploading files to specific folders in PHP, developers can use functions like `uniqid()` to generate unique file names, sanitize user input to prevent malicious file names, and validate file extensions to ensure only allowed file types are uploaded.

// Generate a unique file name using uniqid() and keep the original file extension
$uniqueFileName = uniqid() . '.' . pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

// Sanitize the file name to remove any special characters
$cleanFileName = preg_replace("/[^a-zA-Z0-9.]/", "", $uniqueFileName);

// Validate file extension to only allow certain types of files
$allowedExtensions = array('jpg', 'jpeg', 'png', 'pdf');
$uploadedExtension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

if (!in_array($uploadedExtension, $allowedExtensions)) {
    echo 'Invalid file type. Please upload a JPG, JPEG, PNG, or PDF file.';
    exit;
}

// Move the uploaded file to a specific folder with the sanitized file name
move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $cleanFileName);