I thought my function would have accomplished just that:
if (!function_exists('resetCSV')) {
function &resetCSV($fullFileName, $path, $willClearFile = false) {
/*------------------------------------------------------------------------------------------------------------------------------------------
New 8/23/2006: This function will either reset the second column of a CSV file (clearing the column of values) or clear file if
$willClearFile is true to prevent user data from potentially going to another client
-------------------------------------------------------------------------------------------------------------------------------------------*/
if (!function_exists('check_csv_field_ref')) @include_once(actual_path("$path/functions.inc.php"));
$contents = @file_get_contents(actual_path($fullFileName));
$fileID = @fopen(actual_path($fullFileName), 'r+'); // OPEN FOR READING AND WRITING
if (function_exists('check_csv_field_ref') && is_file(actual_path($fullFileName)) && strlen($contents) > 0) {
if ($willClearFile) { // EMPTY THE FILE ENTIRELY
@fputs($fileID, '');
} else {
while (($data = @fgetcsv($fileID, 65536)) !== false) @array_insert($data, $csvArray[]); // READ EACH ROW UP TO 65K IN DATA
for ($i = 0; $i < @sizeof($csvArray); $i++) {
$csvArray[$i][1] = ''; // NULLIFY (DO NOT UNSET) SECOND COLUMN
@array_walk($csvArray[$i], create_function('&$a', '@check_csv_field_ref(trim($a));'));
fputs($fileID, trim(join(',', $csvArray[$i])));
if ($_ENV['windir'] || $_SERVER['windir']) fputs($fileID, "\r\n"); else fputs($fileID, "\n");
}
}
}
@fclose($fileID);
}
}
However, as it does remove the second column of the CSV file, it fails to overwrite the existing content of the CSV file with the newly-reformed content.
Which is the best approach to accomplish rewriting CSV content this way?
Thanx
Phil