What are some best practices for ensuring data integrity and uniqueness when handling article numbers and associated file uploads in PHP?

Issue: When handling article numbers and associated file uploads in PHP, it is important to ensure data integrity and uniqueness to avoid conflicts and errors. One way to achieve this is by generating a unique identifier for each article number and file upload, such as using a combination of the article number and a timestamp. Additionally, validating the file uploads to ensure they are in the correct format and storing them securely can help maintain data integrity.

// Generate a unique identifier for article numbers and file uploads
$article_number = 'ART' . uniqid();
$timestamp = time();
$upload_filename = $article_number . '_' . $timestamp;

// Validate file uploads
if ($_FILES['file']['error'] === UPLOAD_ERR_OK) {
    $file_extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
    $allowed_extensions = array('pdf', 'doc', 'docx');
    
    if (in_array($file_extension, $allowed_extensions)) {
        move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $upload_filename . '.' . $file_extension);
    } else {
        echo 'Invalid file format. Please upload a PDF or Word document.';
    }
} else {
    echo 'Error uploading file. Please try again.';
}