Link to home
Start Free TrialLog in
Avatar of skaap2k
skaap2k

asked on

How do I make sure a file is a CSV file before i start to try and process it?

Hi,

I have a webapp, and we require people to upload a CSV file for various activities - data eventually gets stored in a DB however.
It all works if you upload a CSV file...
but if I upload file in a different format.. how can I detect this before/during/after processing the file into arrays etc?

Thanks in advance :)
Rob
Avatar of maUru
maUru

depends on how thorough you want to be,

you can try something like:

$delimiter = ',';
$fields = 4; // if this is 0 it will try and 'figure out' the number of fields

$file = file('blabla.csv');
foreach ($file as $key => $line) {
  $numfields = 0;
  $numfields = substr_count($line, $delimiter) + 1;

  if ($key == 0 && $fields > 0) {
        $fields = $numfields;
      }
   }
  if ($numfields != $fields) {
      die('too many fields on line ' . $key ');
  }
}

could be fixed up a bit, but basically it will die if there is an irregularity in the file.
of course this is very very simple, there is no checking for quoted fields, delimeters nested between quotes on fields etc.
Avatar of skaap2k

ASKER

I tried uploading a MS Word doc earlier, and it had decided that there were 2 fields, and 82 rows ...so I dont think that'd work very well, is there perhaps a way I could eliminate other files by working out what they are first - the CSV files i'll be getting dont have a fixed amount of fields or rows..
they dont have a fixed amount of fields? thats not right.

and here is a cleaned up version of what i wrote:

<?

$file = file('test.csv');
$fields = 3; // if this is 0 it will try and 'figure out' the number of fields
if (checkCSV('test.csv', 3)) {
      echo 'file good';
}

function checkCSV($file, $fields=0, $delimiter=',') {
      $delimiter = ',';

      foreach ($file as $key => $line) {
            $numfields = 0;
            $numfields = substr_count($line, $delimiter) + 1;

            if ($key == 0 && $fields == 0) {
                  $fields = $numfields;
            }
            if ($numfields != $fields) {
                  echo 'irregular on line ' . $key;
                  return false;
            }
      }
      return true;
}
?>
ASKER CERTIFIED SOLUTION
Avatar of maUru
maUru

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
if($_FILES['bestand']['type'] == "application/vnd.ms-excel" && eregi('\.csv$', $_FILES['bestand']['name'])
  {
  FILE OK
  }

application/vnd.ms-excel == mime type of the uploaded document
first test all possible mime types by using
print $_FILES['bestand']['type'];

Then of course you have to check the content of that file