$textfile = get_temp_dir(false, $id) . "/" . $collection . "-" . safe_file_name(i18n_get_collection_name($collectiondata)) . $sizetext . ".txt";
     $fh = fopen($textfile, 'w') or die("can't open file");
     fwrite($fh, $text);
     fclose($fh);
     if ($use_zip_extension) {
         $zip->addFile($textfile, $collection . "-" . safe_file_name(i18n_get_collection_name($collectiondata)) . $sizetext . ".txt");
     } else {
         $path .= $textfile . "\r\n";
     }
     $deletion_array[] = $textfile;
 }
 // Include the CSV file with the metadata of the resources found in this collection
 if (getvalescaped('include_csv_file', '') == 'yes') {
     $csv_file = get_temp_dir(false, $id) . '/Col-' . $collection . '-metadata-export.csv';
     $csv_fh = fopen($csv_file, 'w') or die("can't open file");
     $csv_content = generateResourcesMetadataCSV($result);
     fwrite($csv_fh, $csv_content);
     fclose($csv_fh);
     if ($use_zip_extension) {
         $zip->addFile($csv_file, '/Col-' . $collection . '-metadata-export.csv');
     } else {
         $path .= $csv_file . "\r\n";
     }
     $deletion_array[] = $csv_file;
 }
 # Write command parameters to file.
 //update_progress_file("writing zip command");
 if (!$use_zip_extension) {
     $cmdfile = get_temp_dir(false, $id) . "/zipcmd" . $collection . "-" . $size . ".txt";
     $fh = fopen($cmdfile, 'w') or die("can't open file");
     # Remove Windows line endings - fixes an issue with using tar command - somehow the file has got Windows line breaks
<?php

include_once '../include/db.php';
include_once '../include/general.php';
# External access support (authenticate only if no key was provided)
if (getvalescaped('k', '') == '') {
    include_once '../include/authenticate.php';
}
include_once '../include/search_functions.php';
include_once '../include/collections_functions.php';
include_once '../include/csv_export_functions.php';
$search = getvalescaped('search', '');
$restypes = getvalescaped('restypes', '');
$order_by = getvalescaped('order_by', '');
$archive = getvalescaped('archive', '');
$sort = getvalescaped('sort', '');
$starsearch = getvalescaped('starsearch', '');
// Do the search again to get the results back
$search_results = do_search($search, $restypes, $order_by, $archive, -1, $sort, false, $starsearch);
header("Content-type: application/octet-stream");
header("Content-disposition: attachment; filename=search_results_metadata.csv");
echo generateResourcesMetadataCSV($search_results);
ob_flush();
exit;