Advertisement
Guest User

Untitled

a guest
Sep 5th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. if (($file = fopen($csvFile , "r")) != FALSE) {
  2. $ctr = 0;
  3. $record = fgetcsv($file, 1024)) != FALSE) {
  4. if ($ctr == 0) {
  5. correctHeader($record);
  6. # write to new csv.
  7. } else {
  8. # write to new csv.
  9. }
  10. }
  11. }
  12.  
  13. <?php
  14. $from = 'd.csv';
  15. $to = 'd.good.csv';
  16.  
  17. $old = fopen($from, 'r');
  18. if (!is_resource($old)) {
  19. die("Failed to read from source file: $from");
  20. }
  21.  
  22. $headerLine = fgets($old);
  23. $headerLine = fixHeaders($headerLine);
  24.  
  25. $new = fopen($to, 'w');
  26. if (!is_resource($new)) {
  27. die("Failed to write to destination file: $new");
  28. }
  29. // Save the fixed header into the new file
  30. fputs($new, $headerLine);
  31. // Read the rest of old and save to new.
  32. // Old file is already open and we are the second line.
  33. // For large files, reading should probably be done in the loop with chunks.
  34. fwrite($new, fread($old, filesize($from)));
  35.  
  36. // Close files
  37. fclose($old);
  38. fclose($new);
  39.  
  40. // Just an example
  41. function fixHeaders($line) {
  42. return strtoupper($line);
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement