Jump to content

HELP in uploading CSV through php in mysql


rahul19dj

Recommended Posts

I cant get to understand what am I doing wrong here. It keeps saying not a csv file . But I haved saved the file as csv file

 

if($_FILES["file"]["type"] != "application/vnd.ms-excel"){
die("This is not a CSV file.");
}
elseif(is_uploaded_file($_FILES['file']['tmp_name'])){
//Connect to the database
$dbhost = 'localhost';
$dbuser = 'xxxxxxxx';
$dbpass = 'xxxxxxxx';
$dbname = 'xxxxxxxx';
$link = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql server');
mysql_select_db($dbname);

//Process the CSV file
$handle = fopen($_FILES['file']['tmp_name'], "r");
$data = fgetcsv($handle, 1000, ";"); //Remove if CSV file does not have column headings
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
	$att0 = mysql_real_escape_string($data[0]);
	$att1 = mysql_real_escape_string($data[1]);
	$att2 = mysql_real_escape_string($data[2]);
	$att3 = mysql_real_escape_string($data[3]);
	$att0 = mysql_real_escape_string($data[4]);
	$att1 = mysql_real_escape_string($data[5]);
	$att2 = mysql_real_escape_string($data[6]);
	$att3 = mysql_real_escape_string($data[7]);
	$att3 = mysql_real_escape_string($data[8]);
	$sql = "INSERT INTO `tempusers` (
				`fname` ,
				`mobile` ,
				`center` ,
				`balcf` ,
				`used` ,
				`newbal` ,
				`summary` ,
				`date` ,
				`smssend` 

				)
				VALUES ('" . $att0 . "', '" . $att1 . "', '" . $att2 . "', '" . $att3 . "', '" . $att4 . "', '" . $att5 . "', '" . $att6 . "', '" . $att7 . "', '" . $att8 . "')";
	mysql_query($sql);
}
mysql_close($link);
echo "CSV file successfully imported.";
}
else{
die("You shouldn't be here");
}

Link to comment
Share on other sites

what is the value of var_dump($_FILES); ?  According to filext.com, a csv can have types of:

text/comma-separated-values,

text/csv,

application/csv,

application/excel,

application/vnd.ms-excel,

application/vnd.msexcel,

text/anytext

 

you can create an array of those types, and check if the $_FILES['type'] is in_array of it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.