<?php
$dir = "sites/default/files/feeds/csv/gemstones";
$delete_data = "sites/default/files/feeds/csv/gemstones/igl-gemstone.csv";
unlink($delete_data);
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
           // echo "$file" . "\n";
			$mine[]= 'sites/default/files/feeds/csv/gemstones/' . $file;
        }
       
    }
}
sleep(1);
joinCSV($mine,$dh);

function joinCSV($mine,$dh){
// Write file name
$write_data = "sites/default/files/feeds/csv/gemstones/igl-gemstone.csv";
foreach ($mine as $file){
// Read & combine
$fh = fopen($file, 'r');
$data .= fread($fh, filesize($file));
fclose($fh);
}
// Write
$empty_str="";
//file_put_contents($write_data, $empty_str);
$fh = fopen($write_data, 'w+') or die("can't open file");
fwrite($fh, $data);
fclose($fh);
closedir($dh);
}
sleep(1);

deleteCSV($dir);

function deleteCSV($dir){ 
//Get a list of all of the file names in the folder.
$files = glob($dir . '/*');
 
//Loop through the file list.
foreach($files as $file){
    //Make sure that this is a file and not a directory.
    if(is_file($file)){
        //Use the unlink function to delete the file.
		$filename = basename($file);
		if($filename != 'igl-gemstone.csv'){
			unlink($file);
		}
       
    }
  }
}

?>