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
229 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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.
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.

717 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