Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if (($file = fopen($csvFile , "r")) != FALSE) {
- $ctr = 0;
- $record = fgetcsv($file, 1024)) != FALSE) {
- if ($ctr == 0) {
- correctHeader($record);
- # write to new csv.
- } else {
- # write to new csv.
- }
- }
- }
- <?php
- $from = 'd.csv';
- $to = 'd.good.csv';
- $old = fopen($from, 'r');
- if (!is_resource($old)) {
- die("Failed to read from source file: $from");
- }
- $headerLine = fgets($old);
- $headerLine = fixHeaders($headerLine);
- $new = fopen($to, 'w');
- if (!is_resource($new)) {
- die("Failed to write to destination file: $new");
- }
- // Save the fixed header into the new file
- fputs($new, $headerLine);
- // Read the rest of old and save to new.
- // Old file is already open and we are the second line.
- // For large files, reading should probably be done in the loop with chunks.
- fwrite($new, fread($old, filesize($from)));
- // Close files
- fclose($old);
- fclose($new);
- // Just an example
- function fixHeaders($line) {
- return strtoupper($line);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement