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
223 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
Technology Partners: 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Ahax pagination 9 48
How to use session variables in php? 22 48
How do I remove "" from json_encode 5 22
JSON decode 5 19
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

749 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