Link to home
Start Free TrialLog in
Avatar of Mr_Splash

asked on

Upload form - CSV mime type


I've got a form where a csv can be uploaded, part of the upload PHP checks to see if the file is a csv, however I can't seem to get it to work.

Below is my IF statement, which is being entered despite the file being a CSV.

Any ideas?
//Ensure file is csv
  if (!($uploaded_type=="text/csv") && !($uploaded_type=="text/comma-separated-values") && !($uploaded_type=="application/csv") && !($uploaded_type=="application/excel") && !($uploaded_type=="application/") && !($uploaded_type=="application/vnd.msexcel")) {

Open in new window

Avatar of Roonaan
Flag of Netherlands image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mr_Splash


Thanks for your reply,

However my error is like this;
Notice: You are not allowed to upload files of type in /home........
It's not picking up the file at all?

I've put the rest of my code below, any idea what I'm doing wrong?

I've also upped the points as things are now more complicated.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Import Attendees</title>
if ($_POST['action'] == "import") {
  ////// Handle Upload //////
  $target = "temp/";
  $file = basename( $_FILES['uploaded']['name']);
  $target = $target.$file;
  $allowedTypes = array();
  $allowedTypes[] = "text/csv";
  $allowedTypes[] = "text/comma-separated-values";
  $allowedTypes[] = "application/csv";
  $allowedTypes[] = "application/excel";
  $allowedTypes[] = "application/";
  $allowedTypes[] = "application/vnd.msexcel";
  if(!in_array($uploaded_type, $allowedTypes)) {
    $ok = 0;
    // Show a php error in your log or on screen
    trigger_error("You are not allowed to upload files of type $uploaded_type", E_USER_NOTICE);
  if ($ok==0) {
    Echo "Sorry, the file must be a csv.</br><br/><a href=\"import-upload.php\">Try Again.</a>";
  if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) {
    echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded<br/>";
  else {
    echo "Sorry, there was a problem uploading your file.</br><br/><a href=\"import-yesno-yearsattended.php\">Try Again.</a>";
else { ?>
  <h2>Upload CSV</h2>
  <table cellpadding="0" cellspacing="5" border="0">
  <form method="post" action="import-upload.php" enctype="multipart/form-data">
    <tr><td width="50">Year : </td><td><input type="text" value="YYYY" name="year" maxlength="4" /></td></tr>
    <tr><td>CSV : </td><td><input type="file" name="csv" /></td></tr>
    <tr><td></td><td><input type="submit" value="Import" /></td></tr>
    <input type="hidden" value="import" name="action" /> 
<?php }

Open in new window

Got it!

file input had a different name than that in the code.