Page 1 of 1

Determine File Type of CSV

Posted: Mon Oct 10, 2005 8:17 am
by kendall
Hello,

i have built an application that allows someone to upload "CSV" formatted files to be parsed so that the contents can be inserted into a databaste

The following code detects if the file uplaoded is a csv

Code: Select all

if(($filetype != 'application/vnd.ms-excel')){
		 $Msg .= 'Error: Could not Upload - '.$filename.' Only CSV '.$filetype.' formats are accepted';
     }else{
            if(file_exists($filename)){
               @unlink($filename);
            }
            move_uploaded_file( $tmp_name, $filename );
        }
The client is using MS Xcel as thier csv client application

The problem is i note that i get different file type readings when upload csv files.
application/octet-stream
application/vnd.ms-excel
text/csv
text/comma-separated-values
Which is the default csv file type to be using...note that the client is using excel 2003 and converting xecel spredsheet into comma delimited csv file format

Kendall

Posted: Mon Oct 10, 2005 8:33 am
by Jenk
You can use mime_content_type() for files you already have one the server, or (I can't quite remember the keys so it might be different) $_FILES['tmpfile']['type'] to see what type the user is uploading.

BTW, I think the first two on your list are infact *.xls, which is not CSV anyway.

Determine File Type of CSV

Posted: Mon Oct 10, 2005 8:38 am
by kendall
BTW, I think the first two on your list are infact *.xls, which is not CSV anyway.
If they are then what should i really specify?

the client is converting xcel data into csv so i guess i should use one of the first 2?

but then I have used xcel to convert into csv and i get the 3 mime type coming up

I am confused

:?:

Posted: Mon Oct 10, 2005 8:43 am
by Jenk
CSV is not an excel specific thing, it just means literally what it says, a file of Comma seperated values.

Though you don't have to use commas of course.

The last two is what you should be using.

Code: Select all

<?php

$types = array('text/csv', 'text/comma-seperated-values');

if (!in_array($_FILES['tmpfile']['type'], $types)) {
  $msg = "Error! Incorrect file type!";
}

?>

Determine File Type of CSV

Posted: Mon Oct 10, 2005 8:46 am
by kendall
Then why would a xcel sheet converted to CSV format not give a filte type reading of application/octet-stream or application/vnd.ms-excel ? :?:

Posted: Mon Oct 10, 2005 8:53 am
by Jenk
Because it will be saved as a CSV file, with the mime-type of either text/csv or text/comma-seperated-values.

Posted: Mon Oct 10, 2005 8:55 am
by kendall
Actually i meant it the other way around

when i saved it as a CSV file the file type read application/octet-stream or application/vnd.ms-excel :?:

Posted: Mon Oct 10, 2005 9:24 am
by Jenk
the wonders of MS prducts..

I'm not sure then, will have a look around to see if there is a pre-made script.