Link to home
Start Free TrialLog in
Avatar of Mr_Splash
Mr_Splash

asked on

Upload form - CSV mime type

Hi,

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/vnd.ms-excel") && !($uploaded_type=="application/vnd.msexcel")) {
    $ok=0;
  }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Link to home
membership
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
Mr_Splash

ASKER

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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Import Attendees</title>
</head>
 
<body>
<?php
if ($_POST['action'] == "import") {
 
  ////// Handle Upload //////
  $target = "temp/";
  $file = basename( $_FILES['uploaded']['name']);
  $target = $target.$file;
  $ok=1;
  
  $allowedTypes = array();
  $allowedTypes[] = "text/csv";
  $allowedTypes[] = "text/comma-separated-values";
  $allowedTypes[] = "application/csv";
  $allowedTypes[] = "application/excel";
  $allowedTypes[] = "application/vnd.ms-excel";
  $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>";
	exit();
  } 
 
  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>";
	exit();
  }
}
 
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" /> 
  </form>
  </table>
<?php }
 
?>
</body>
</html>

Open in new window

Got it!

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