Determine File Type of CSV

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Determine File Type of CSV

Post 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
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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.
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Determine File Type of CSV

Post 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

:?:
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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!";
}

?>
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Determine File Type of CSV

Post 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 ? :?:
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Because it will be saved as a CSV file, with the mime-type of either text/csv or text/comma-seperated-values.
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Post 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 :?:
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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.
Post Reply