Solved

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

Posted on 2006-07-20
8
208 Views
Last Modified: 2006-11-18
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
0
Comment
Question by:skaap2k
  • 4
8 Comments
 
LVL 7

Expert Comment

by:maUru
ID: 17148676
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.
0
 
LVL 7

Expert Comment

by:maUru
ID: 17148696
of course this is very very simple, there is no checking for quoted fields, delimeters nested between quotes on fields etc.
0
 
LVL 5

Author Comment

by:skaap2k
ID: 17148737
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..
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 7

Expert Comment

by:maUru
ID: 17148773
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;
}
?>
0
 
LVL 7

Accepted Solution

by:
maUru earned 500 total points
ID: 17148793
sorry that was wrong:

<?

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

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

      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;
}
?>
0
 

Expert Comment

by:madman2
ID: 17199097
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
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question