Guest User

Untitled

a guest
Sep 14th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. why the code can't update the content in the csv?
  2. $dir = getcwd();
  3. $files = scandir($dir);
  4. foreach ($files as $file)
  5. {
  6. $parts = pathinfo($file);
  7. if($parts['extension']!="csv") continue;
  8. if (($handle = fopen($file, "r")) !== FALSE)
  9. {
  10. $new_content = implode(',', fgetcsv($handle, 1000, ","))."n";
  11. while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
  12. {
  13. $data[12]=substr($data[12],9);
  14. $data[13]=substr($data[13],9);
  15. $data[14]=substr($data[14],9);
  16. $data[42]=888;
  17. $new_content .= implode(',', $data)."n";
  18. }
  19. fclose($handle);
  20. file_put_contents($file, $new_content);
  21. }
  22. }
  23.  
  24. $data = fgetcsv($handle, 0, ','); // >= PHP 5.0.4
  25.  
  26. $data = fgetcsv($handle, 4096, ',');
  27.  
  28. $dir = getcwd();
  29. $files = scandir($dir);
  30. foreach ($files as $file) {
  31. $parts = pathinfo($file);
  32. if ($parts['extension']!="csv") {
  33. continue;
  34. }
  35. if (false === ($outfile = fopen("$file.tmp", "w"))) {
  36. continue;
  37. }
  38. if (($handle = fopen($file, "r")) !== FALSE) {
  39. fputcsv($outfile, fgetcsv($handle, 4096, ","));
  40.  
  41. while (($data = fgetcsv($handle, 4096, ",")) !== FALSE) {
  42. $data[12]=substr($data[12],9);
  43. $data[13]=substr($data[13],9);
  44. $data[14]=substr($data[14],9);
  45. $data[42]=888;
  46. fputcsv($outfile, $data);
  47. }
  48. fclose($handle);
  49. fclose($outfile);
  50. if (false === rename("$file.tmp", $file)) {
  51. die("Could not rename temporary file");
  52. }
  53. }
  54. }
Add Comment
Please, Sign In to add comment