7

I need help with reading the folder and opening / outputting the csv data, I have used examples other people have written but none of them have worked for me.

What I currently have is this, but it doesn't output the files:

$files = scandir($PathToCreate.$version."/"); //scan the folder
foreach($files as $file) { //for each file in the folder

//The following is another example I found but does not output anything I just need to open each file and be able to output / target specific data

$csv = array();
$lines = file($file, FILE_IGNORE_NEW_LINES);

foreach ($lines as $key => $value)
{
    $csv[$key] = str_getcsv($value);
} 
print_r($csv)

}
Rizier123
  • 58,877
  • 16
  • 101
  • 156
Dgeorgex000000
  • 93
  • 1
  • 2
  • 8
  • What delimiter does your csv file has? – Rizier123 Apr 13 '15 at 15:07
  • 1
    "none of them have worked" does not help. What is your issue? – arkascha Apr 13 '15 at 15:11
  • It uses commas to seperate infomation – Dgeorgex000000 Apr 13 '15 at 15:11
  • And what output do you get right now and what would you expect? – Rizier123 Apr 13 '15 at 15:12
  • BTW: take a look at the `fgetcsv()` function: http://php.net/manual/en/function.fgetcsv.php – arkascha Apr 13 '15 at 15:14
  • Sorry im new to stack overflow , my issue is that when using scan directory it correctly shows the right amount and names of files but I am unable to successfully read each file and collect the data / use the data, it is beyind my abilities and when other people have answered on this problem through various sources none of there solutions have worked for me as when I echo the code nothing happens – Dgeorgex000000 Apr 13 '15 at 15:14
  • You _still_ did not say what the real issue is. "I am unable" is to vague again. What error do you get? What happens? Don't have us drag all details out of your nose! :-))) – arkascha Apr 13 '15 at 15:15
  • Ok sorry again, for a better example I took a snippet from w3 schools that should give a basic example of what i am trying to do so now my code reads '$files = scandir($PathToCreate.$version."/"); foreach($files as $file) { $csvfile = fopen($file,"r"); print_r(fgetcsv($csvfile)); fclose($csvfile); }' which should print the lines but all I get is a blank screen – Dgeorgex000000 Apr 13 '15 at 15:18
  • Why do you throw examples at us? Say what your issue is with the above code. What is the output? You did not tell! What is the error you get? What does the http server's error log file say? – arkascha Apr 13 '15 at 15:20
  • My issue is it is not displaying any data nothing is being output all I need to do is output the infomation and nothing is happening – Dgeorgex000000 Apr 13 '15 at 15:21
  • Once more: any errors you get, especially in the error log file? I insist, because you want to learn how to debug yourself. For that you have to find out the actual error you are dealing with. You will have to learn how to debug yourself. – arkascha Apr 13 '15 at 15:24

2 Answers2

9

This should work for you:

(Here I first grab all files out of the directory which have the extension *.csv with glob(). After this I loop through each file and read it with fopen() and fgetcsv().)

<?php

    $files = glob("$PathToCreate$version/*.csv");

    foreach($files as $file) {

        if (($handle = fopen($file, "r")) !== FALSE) {
            echo "<b>Filename: " . basename($file) . "</b><br><br>";
            while (($data = fgetcsv($handle, 4096, ",")) !== FALSE) {
                echo implode("\t", $data);
            }
            echo "<br>";
            fclose($handle);
        } else {
            echo "Could not open file: " . $file;
        }

    }

?>
Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • Omg that is perfect thank you, is there a way to target specific data by its header? – Dgeorgex000000 Apr 13 '15 at 15:24
  • @Dgeorgex000000 You're welcome! (*is there a way to target specific data by its header* What exactly do you mean with that?) – Rizier123 Apr 13 '15 at 15:25
  • Each row of data in the csv is under a header / heading , is there a simple way to only select data from certain headings so for instance just the infomation in row 4 or just the infomation under heading xxx – Dgeorgex000000 Apr 13 '15 at 15:31
  • @Dgeorgex000000 Instead of: `echo implode("\t", $data);` do `echo $data[3];` Is this what you mean? – Rizier123 Apr 13 '15 at 15:38
  • 1
    That was one fiery sex tornado of an answer, could not have been more perfect thank you so much – Dgeorgex000000 Apr 13 '15 at 15:40
0

The first issue probably is that you have to ignore the two directory entries . and .. which have special meaning and are not useful for you:

$files = scandir($PathToCreate.$version."/"); //scan the folder
foreach($files as $file) { //for each file in the folder
  if ( ! in_array($file, ['.','..'])) {
    $lines = file($file, FILE_IGNORE_NEW_LINES);
    foreach ($lines as $key => $value) {
      $csv[$key] = str_getcsv($value);
    } 
    print_r($csv)
  }
}
arkascha
  • 41,620
  • 7
  • 58
  • 90
  • 1. To check for each file if it is neither `.` nor `..` for the entire loop isn't very clear 2. `$csv = fgetcsv();` what? – Rizier123 Apr 13 '15 at 15:19
  • @Rizier123 about 2: sorry, accidental paste :-( Thanks for pointing that out. – arkascha Apr 13 '15 at 15:22
  • @Rizier123 About 1: "isn't very clear"? There are certainly multiplay ways to do this, you are using `glob`, fine too. But what is "not clear" with this condition? – arkascha Apr 13 '15 at 15:23
  • Let's say we would have 10k files. Then it isn't very clever to check each file if it is neither `.` nor `..` you would better filter them before the loop out with `array_diff()` and this would also help the performance! – Rizier123 Apr 13 '15 at 15:24
  • @Rizier123 I doubt that is an issue here :-) The OP mentioned uploaded files. He'd be very busy to upload 10k files :-) But from a theoretical point of view you are right. – arkascha Apr 13 '15 at 15:27