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
185 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now