samedi 7 février 2015

Unwanted value in my csv file when I save this file with php response

I created a new .csv file and I save on server and everythink it's ok. But when I want to save this file on hard drive using php response, I have only one line in csv:



<html><head><meta charset='utf-8'><table><tr><td>Upload: </td><td>plik2.csv</td><td>Upload: </td><td>plik1.csv</td></tr><tr><td>Type: </td><td>application/vnd.ms-excel</td><td>Type: </td><td>application/vnd.ms-excel</td></tr><tr><td>Size: </td><td>7.330078125 Kb</td><td> Size: </td><td>8.802734375 Kb</td></tr><tr><td>Temp file: </td><td>C:\wamp\tmp\php4AC3.tmp</td><td> Temp file: </td><td>C:\wamp\tmp\php4AC4.tmp</td></tr></table>Stored in: nowe/plik2.csv<br />Stored in: nowe/plik1.csv<br />Filed opened.<br />


This is my code:



<?php
header('Content-Description: File Transfer');
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/csv; name=output.csv");
header("Content-Disposition: attachment;filename=output.csv");
header("Content-Type: text/html; charset=UTF-8");
echo "<html><head><meta charset='utf-8'>";
if ( isset($_POST["submit"]) ) {

if ( isset($_FILES["file"]) && isset($_FILES["file2"])) {

//if there was an error uploading the file
if (($_FILES["file"]["error"] > 0) && ($_FILES["file2"]["error"] > 0)){
echo "Return Code: " . $_FILES["file"]["error"] . " " .$_FILES["file2"]["error"] . "<br />";

}
else {
//Print file details
echo "<table>";
echo "<tr><td>Upload: </td><td>" . $_FILES["file"]["name"] . "</td><td>Upload: </td><td>" . $_FILES["file2"]["name"] . "</td></tr>";
echo "<tr><td>Type: </td><td>" . $_FILES["file"]["type"] . "</td><td>Type: </td><td>" . $_FILES["file2"]["type"] . "</td></tr>";
echo "<tr><td>Size: </td><td>" . ($_FILES["file"]["size"] / 1024) . " Kb</td><td> Size: </td><td>" . ($_FILES["file2"]["size"] / 1024) . " Kb</td></tr>";
echo "<tr><td>Temp file: </td><td>" . $_FILES["file"]["tmp_name"] . "</td><td> Temp file: </td><td>" . $_FILES["file2"]["tmp_name"] . "</td></tr>";
echo "</table>";

//if file already exists
if (file_exists("upload/" . $_FILES["file"]["name"]) && file_exists("upload/" . $_FILES["file2"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
echo $_FILES["file2"]["name"] . " already exists. ";
}
else {
//Store file in directory "upload" with the name of "uploaded_file.txt"
$storagename = '/nowe';
move_uploaded_file($_FILES["file"]["tmp_name"], $storagename);
move_uploaded_file($_FILES["file2"]["tmp_name"], $storagename);
echo "Stored in: " . "nowe/" . $_FILES["file"]["name"] . "<br />";
echo "Stored in: " . "nowe/" . $_FILES["file2"]["name"] . "<br />";
}
}
} else {
echo "No file selected <br />";
}
}
if ($_POST["rodzaj"] == 'full'){
if ( ($file = fopen($storagename , "r" )) && ($file2 = fopen($storagename , "r" ))) {
echo "Filed opened.<br />";
$db1 = new SQLite3(":memory:");

// create first table with first csv file
$i = 0;
while (($fields1[$i] = fgetcsv ($file, 0, ";" )) !== false) {

$i++;
}
$counter = count($fields1[0]);
$counter2 = count($fields1);
$statement1 = "CREATE TABLE tabela1 (";
$statement2="";
for($i = 1; $i < count($fields1[0])+1; $i++){
$statement2 = $statement2 ." k".$i. " text,\n";
}
$statement2 = trim($statement2, ",\n");
$statement3 = ");";
$statement = $statement1 . $statement2 . $statement3;
$db1->exec($statement);

for($i = 1; $i < count($fields1); $i++){
$st1 = "INSERT INTO tabela1 VALUES (";
$st2 = "";
for($j = 0; $j < count($fields1[0]); $j++){
//$fields1[$i][$j] = iconv ('CP1250', 'UTF-8', $fields1[$i][$j]);
$st2 = $st2 . "'".$fields1[$i][$j]."',";
}
$st2 = trim($st2, ",\n");
$st3 = ");";
$st = $st1 . $st2 . $st3;
$db1->exec($st);
}

//*******************************************************************//

// create second table with second csv file
$i = 0;
while (($fields2[$i] = fgetcsv ($file2, 0, ";" )) !== false) {
$i++;
}
$counter = count($fields2[0]);
$counter2 = count($fields2);
$statement1 = "CREATE TABLE tabela2 (";
$statement2="";
for($i = 1; $i < count($fields2[0])+1; $i++){
$statement2 = $statement2 ." k".$i. " text,\n";
}
$statement2 = trim($statement2, ",\n");
$statement3 = ");";
$statement = $statement1 . $statement2 . $statement3;
$db1->exec($statement);

for($i = 1; $i < count($fields2); $i++){
$st1 = "INSERT INTO tabela2 VALUES (";
$st2 = "";
for($j = 0; $j < count($fields2[0]); $j++){
//$fields2[$i][$j] = iconv ('CP1250', 'UTF-8', $fields2[$i][$j]);
$st2 = $st2 . "'".$fields2[$i][$j]."',";
}
$st2 = trim($st2, ",\n");
$st3 = ");";
$st = $st1 . $st2 . $st3;
$db1->exec($st);

}
}

$fp = fopen('./output.csv', 'w');
$naglowki = array_merge($fields1[0], $fields2[0]);
fputcsv($fp, $naglowki);
$joinwynik = $db1->query("SELECT * FROM tabela1 INNER JOIN tabela2 ON tabela1.k2 = tabela2.k2");
while ($row = $joinwynik->fetchArray(SQLITE3_NUM)){
fputcsv($fp, $row);
}
fclose($fp);
} ...
?>


If somebody can show me my mistake I will be really grateful.


Aucun commentaire:

Enregistrer un commentaire