1

Variables:

$csvFile = $_FILES['uploadfile']['tmp_name'];
$csvFile = $con->real_escape_string($csvFile);
$mimes = array('application/vnd.ms-excel','text/plain','text/csv','text/tsv','csv');

if statement:

if(in_array($csvFile, $mimes))
{
$file = fopen($csvFile, 'r');
$i = 0;
$row = 1; 
while (($line = fgetcsv($file)) !== FALSE) 
{
if($row == 1){ $row++;  continue; }
$data_array[$i] = $line[0];
$data_array[$i] = mysqli_real_escape_string($con, trim($data_array[$i]));
$encrypted_numbers[$i] = encryption::encrypt($data_array[$i]);
$encrypted_numbers[$i] .= mysqli_real_escape_string($con, trim($line[1]));
$i++;
}
fclose($file);
} 
else 
{
die("Sorry, file type not allowed");
};

I've been trying stuff for 30 min, anyone know why I always get false even if the file is named csv.csv?

hakre
  • 193,403
  • 52
  • 435
  • 836
Gjorg
  • 25
  • 6
  • what does var_dump($csvFile) before the in_array call give you ? – Maximus2012 Aug 12 '13 at 01:27
  • most likely $csvFile will give you the actual file name and not the mime type for that file which is what you are trying to do with in_array. – Maximus2012 Aug 12 '13 at 01:29
  • @Maximus2012 string(26) "D:\\xampp\\tmp\\phpAC6.tmp" – Gjorg Aug 12 '13 at 01:30
  • @user2671532 this will always result in in_array returning false when you try to match it with $mimes. – Maximus2012 Aug 12 '13 at 01:31
  • @Maximus2012 I realize that, and I tried having csvFile without a temp name and then imploding it, still got false even though var_dump gave me "csv.csv" name – Gjorg Aug 12 '13 at 01:32
  • you need to get the mime type for the file. See if this helps: http://stackoverflow.com/questions/134833/how-do-i-find-the-mime-type-of-a-file-with-php – Maximus2012 Aug 12 '13 at 01:32

1 Answers1

1
$_FILES['uploadfile']['tmp_name'];

you should be looking in 'type' not 'tmp_name'

$_FILES['uploadfile']['type'];

However, there is an SO discussion here about why a csv file can have the application/octet-stream mime-type. It suggests that you need to verify the file-type yourself, and not rely on ['type']. The best way would be to use fgetcsv() to try to parse it as csv. If this fails then assume it is not a csv.

Community
  • 1
  • 1
Andy G
  • 19,232
  • 5
  • 47
  • 69