How can the "flock" function be utilized in PHP to manage file locking and prevent data corruption in a scenario where multiple users are downloading files concurrently?

To prevent data corruption when multiple users are downloading files concurrently, the "flock" function in PHP can be utilized to manage file locking. By using this function, we can ensure that only one user can access a file at a time, preventing conflicts and data corruption.

$filename = "example.txt";

$handle = fopen($filename, "r");

if (flock($handle, LOCK_SH)) {
    // Perform file read operations here
    
    flock($handle, LOCK_UN); // Release the lock
} else {
    echo "Could not lock file!";
}

fclose($handle);