How can fseek be used in conjunction with fwrite in PHP to manage file pointers when writing specific lines to a new file?

When writing specific lines to a new file in PHP, you can use fseek to move the file pointer to the desired position before writing with fwrite. This allows you to overwrite or insert data at a specific location in the file.

// Open the source file for reading
$source = fopen('source.txt', 'r');

// Open the destination file for writing
$destination = fopen('destination.txt', 'w');

// Move the file pointer to the desired position (e.g. line 3)
fseek($source, 2); // Assuming each line is terminated with a newline character

// Read the line from the source file
$data = fgets($source);

// Write the line to the destination file
fwrite($destination, $data);

// Close the files
fclose($source);
fclose($destination);