logo   PHP Upload fra CSVfil.




Hent landekoder


Henter landekoder (landekode og landenavn separeret med ;) til indlæggelse i array. Arrayen løftes til sidst op i $_SESSION.


// Henter landekoder til $_SESSION[landekoder]
$file1=fopen("diverse/landekoder.csv","r");
$array1 = fgetcsv($file1);
$landekoder = array();
while(($array1=fgetcsv($file1))!==FALSE)
{
$temp_array = explode(';',$array1[0]) ;
$key = $temp_array[0];
$value = $temp_array[1];
$landekoder[$key] = $value;
}
$_SESSION['landekoder'] = $landekoder;




- - - - - - - - - - - - -

Eksterne .csv ("kommaseparerede") filer kan være:

  • Eget site
    Filen ligger indenfor egen site-folder. Hentes ved angivelse af sti.
  • localhost
    Filen ligger indenfor egen local host. !!!!Hentes ved angivelse af sti.
  • Ekstern
    Filen er placeret på en ekstern server. Hentes med FTP.




Eget site


Den første opgave i forbindelse med læsning af en intern csv-fil er at få "hul igennem". Det kan gøres med denne PHP-kode:


<?php
$row = 1;
if (($handle = fopen("landekoder.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 100, ";")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
?>


Sti til filen, længden på den enkelte datastreng og separatoren skal tilpasses den enkelte situation.


Hvad der der efterfølgende skal ske med de data, der er etableres forbindelse til vil være individuelt og ikke blive behandlet her.




local host


CSV-fil kan læses fra localhost med følgende kode:


<?php

/********************************/
/* Code at http://legend.ws/blog/tips-tricks/csv-php-mysql-import/
/* Edit the entries below to reflect the appropriate values
/********************************/
$databasehost = "localhost";
$databasename = "dbname";
$databasetable = "dbtable";
$databaseusername ="username";
$databasepassword = "password";
$fieldseparator = ",";
$lineseparator = "\n";
$csvfile = "filenamewithtimestamp.csv";
/********************************/
/* Would you like to add an empty field at the beginning of these records?
/* This is useful if you have a table with the first field being an auto_increment integer
/* and the csv file does not have such as empty field before the records.
/* Set 1 for yes and 0 for no. ATTENTION: don't set to 1 if you are not sure.
/* This can dump data in the wrong fields if this extra field does not exist in the table
/********************************/
$addauto = 0;
/********************************/
/* Would you like to save the mysql queries in a file? If yes set $save to 1.
/* Permission on the file should be set to 777. Either upload a sample file through ftp and
/* change the permissions, or execute at the prompt: touch output.sql && chmod 777 output.sql
/********************************/
$save = 0;
$outputfile = "output.sql";
/********************************/

if(!file_exists($csvfile)) {
echo "File not found. Make sure you specified the correct path.\n";
exit;
}

$file = fopen($csvfile,"r");

if(!$file) {
echo "Error opening data file.\n";
exit;
}

$size = filesize($csvfile);

if(!$size) {
echo "File is empty.\n";
exit;
}

$csvcontent = fread($file,$size);

fclose($file);

$con = @mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error());
@mysql_select_db($databasename) or die(mysql_error());

$lines = 0;
$queries = "";
$linearray = array();

foreach(split($lineseparator,$csvcontent) as $line) {

$lines++;

$line = trim($line," \t");

$line = str_replace("\r","",$line);

/************************************
This line escapes the special character. remove it if entries are already escaped in the csv file
************************************/
$line = str_replace("'","\'",$line);
/*************************************/

$linearray = explode($fieldseparator,$line);

$linemysql = implode("','",$linearray);

if($addauto)
$query = "insert into $databasetable values('','$linemysql');";
else
$query = "insert into $databasetable values('$linemysql');";

$queries .= $query . "\n";

@mysql_query($query);
}

@mysql_close($con);

if($save) {

if(!is_writable($outputfile)) {
echo "File is not writable, check permissions.\n";
}

else {
$file2 = fopen($outputfile,"w");

if(!$file2) {
echo "Error writing to the output file.\n";
}
else {
fwrite($file2,$queries);
fclose($file2);
}
}

}

echo "Found a total of $lines records in this csv file.\n All records imported";

?>




















x
x