1

I'm planning to run a php program from Mac Terminal. I have a folder on my desktop with around 800 .csv files and I need to write a php program that reads through and reads each one so that I can run some transformations on the data it's storing. I know how to parse the .csv once it's loaded but I'm wondering if there is a way to load each file without having to name it explicitly? I don't have a list of the 800 file names but I feel like there has to be a way to just read in all the files in a folder in a loop or something without having the title of each file listed -- I don't have much coding experience, so forgive me if there's an obvious answer of which I'm oblivious.

Thank you!

Jennifer
  • 41
  • 2
  • possible duplicate of [Can PHP's glob() be made to find files in a case insensitive manner?](http://stackoverflow.com/questions/2520612/can-phps-glob-be-made-to-find-files-in-a-case-insensitive-manner) – Gordon Feb 25 '12 at 17:57
  • possible duplicate of [How to extract Data from a CSV File](http://stackoverflow.com/questions/2805427/how-to-extract-data-from-csv-file-in-php/2805486#2805486) – Gordon Feb 25 '12 at 17:58
  • even without any coding experience: please use the search function before asking please. – Gordon Feb 25 '12 at 17:59
  • I did try finding the answer via searching, which has worked for a lot of my questions. With this, I was mostly looking for how to get all the files from a folder and I think, because, as I now realize, programmers would refer to a folder as a directory, all my searches for information about "functions for loading all files in a folder" didn't really return anything useful. Otherwise, hopefully, I would have been able to come across the glob() and readdir() functions without having to post a question and clog up the forum. Anyway, apologies! – Jennifer Feb 25 '12 at 21:13

2 Answers2

1

There are a few way todo this but glob'ing is very straightforward:

<?php
foreach (glob("*.csv") as $filename) {
   //do somthing
}
?> 
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
  • I just checked out that function and it's definitely going to help with this code. Thanks so much! :D – Jennifer Feb 25 '12 at 17:07
1

You can loop through all files in a directory using readdir() :http://php.net/manual/en/function.readdir.php.

Once you get the file name using readdir() you can parse it by either breaking the file content into an array and working with the cells by looping through the array using str_getcsv() (requires at least phpv5.3) or the old fashion fgetcsv() to read through the file one line at a time. For each file create a string variable, and after line you read through and transform, simply append the modified line to this string with an end-of-line character appended as well. After reading through the entire file, simply replace the file contents of the original with file_put_contents()

Ben D
  • 14,321
  • 3
  • 45
  • 59
  • Awesome! I'll read up on those functions and that should get me started with my code. Thank you so much! :D – Jennifer Feb 25 '12 at 16:52