Jump to content

modify csv contents


jrobles

Recommended Posts

I am uploading a CSV file and loading the contents into a mysql table. The CSV contains credit card info so I am encrypting the cc string before i insert it into the table. That all works fine, but I have to keep a copy of the original CSV file on my server and I dont want a CSV file siting there with a bunch of credit card numbers in it, just waiting to get stolen. How can I replace the CC string inside the CSV after i insert the records and before I fclose()?

 

here is some of my code:

//UPLOAD THE FILE BEING PROCESSED
$uploaddir = 'uploaded_files/';
$uploadfile = $uploaddir . basename($_FILES['file']['name']);
move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile);

//RENAME THE FILE
$rand_num = mt_rand(5,9999999999);
$new_file = $uploaddir. date("mdY"). $rand_num.".csv";
rename($uploadfile,$new_file);

//DECLARE $HANDLE AS OPEN .CSV FILE
$handle = fopen("$new_file", "r");

//INSERT RECORDS WITH CC VALIDATION 
$cnt = 0;
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{	
//INSERT INTO INVENTORY TABLE
$import="INSERT INTO inventoryTable(customerid,imbatchid, importdate,fname, mname, lname, phone, address1, address2, city, state, zip) VALUES(CONCAT(substring('$data[2]',1,1),substring('$data[4]',1,3),substring('$data[0]',1,1)),'$batchid',NOW(),'$data[0]','$data[1]','$data[2]', '$data[3]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]')";
mysql_query($import) or die(mysql_error());

//VALIDATE CC
if (checkCreditCard ($data[9], $data[10], $ccerror, $ccerrortext)) 
{$ccerrortext = 'This card has a valid format';
$ccerror=9;}

//ENCRYPT CC NUMBER
$cc = encrypt($data[9]);

//INSERT CC DATA
$cc_query="INSERT INTO creditCards(customerID,CardNumber,ValidationStatus,ValidationDesc,ExpireMonth,ExpireYear,CardType)VALUES(CONCAT(substring('$data[2]',1,1),substring('$data[4]',1,3),substring('$data[0]',1,1)),'$cc',$ccerror,'$ccerrortext','$data[11]','$data[12]','$data[10]')";
mysql_query($cc_query) or die(mysql_error());
}

//CLOSE THE .CSV FILE
fclose($handle);

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.