Hi traq.
Thanks for the reply. This csv uploader allows the user to upload any csv file, with any arrangement of columns. Then the very basic csv class will display the uploaded csv and database headers in a new html table. The user can then map name and email and phone etc to the db columns.. Then the info is saved to the db.. The function above allows me to put the users id into the newly uploaded users csv file... and then create a new csv file named after the users id... This is then parsed into the database.. The problem is it uses a "LOAD DATA LOCAL INFILE" function to load the db.. and so duplicates are added. I am trying to remove the duplicates from the uploaded file, before it is parsed to the csv class file and entered into the db..
Code:
$seperators = $_POST['field_separate_char'];
# is this an uploaded file?
# the path to an uploaded file is available in the $_FILES superglobal. why are you trying to parse it here?
$our_name = $tempdir.'/'.basename($_POST['fnames']);// is the uploaded file
# why basename()? does your $User->id contain directory separators?
$new_file = $tempdir.'/'.basename($User->id.'.csv');// no seperators but this works
# there is a built-in php function for reading a file into an array of lines:
# http://php.net/file
# if the files are large, you can save a LOT of memory by reading only one line at a time:
# http://php.net/fgets
$nlines = explode("\r\n",file_get_contents($our_name));
# also, here, you're splitting lines on "\r\n", but further down, you use "\n" as an EOL marker.
# one or the other or both of these will eventually fail when given a file with an unexpected line ending character.
# if this method gets the names of the field headers,
# then I would expect the class to also provide a way to select a specific header field...? // no way to get a header field, i would have to find the @ to determin the email..
# this would solve your problem.
$arr_head = CSV::get_header_fields( $db, $our_name, 'utf8', $seperators, '"', '\\' );
# also, you have an extra semicolon at the end of that line
$num = count($arr_head);
$nwFile = fopen($new_file, 'w');
# why is your "newarray" a string? // it is a string because i just added the user id to the end and then rewrote it to a new csv file to later parse to the csv class file to handle the db input
$newarray ='';
# this is redundant: foreach operates on the array values by default anyway: it doesnt work other wise to give me the value only in a string format
foreach (array_values($nlines) as $value){
# instead of checking if the value is empty, wouldn't it make more sense to check if the $separator is present? // ? this works too
if($value != '') {
$nnmu = explode($seperators, $value);
// here i would like to find a the email in every line and then run a query to the db, if the email exists exclude the line from the $newarray below
# as I pointed out above, check whether your CSV class already has a method that finds the email field. i dont have that function in the db :-(
$totnum = count($nnmu);
if($totnum == $num){ // here we seperate the uneven lines from the csv file, causes issues with alignment of data to columns
# writing lines to the file manually is error-prone. I mentioned in your other thread, php has built-in functions for using csv-formatted files:
# http://php.net/fgetcsv
# http://php.net/fputcsv
# I would assume the CSV class you're using would have similar methods.
$newarray .= $value.','.$User->id."\n"; // here we attach a new or multiple columns to the csv line..
}
}
}
fwrite($nwFile,$newarray);
fclose($nwFile);
unlink($our_name);
I would imagine to add a query somewhere to find emails in the imploded array.. Can you check my function above? This entire function is working for me accept the duplicates in the db.. thanks for the good tips otherwise i did fix some now..
Bookmarks