1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
<?php
include ("../scriptsPHP/db.php");
include ("../scriptsPHP/connect.php");
$repTemp = "./";
$repMysql= "/var/www/tmp/";
$query0 = "SHOW COLUMNS FROM $tbl ";
$result0 = mysql_query($query0) or die ("Query to show columns from table Failed!");
if (isset($DOWNLOAD)){
if ($DOWNLOAD == '1'){
if (isset($_POST['downloadAll'])){
$nameFile = "allRecords.ath";
$tempFile = $repTemp.$nameFile;
$mysqlFile = $repMysql.$nameFile;
$query2 = "SELECT * INTO OUTFILE '$mysqlFile' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\\n' FROM $tbl ORDER BY id ASC";
$result2 = mysql_query ($query2) or die ("Query Failed download all records.\n<br />Query: " . $query2 . "<br />\nError: (" . mysql_errno() . ") " . mysql_error());
if (file_exists($mysqlFile)){
if (copy($mysqlFile, $tempFile)){
//formatage du fichier:
//recupération de la ligne des variable
$cpt = 0;
$newFileContent = "";
while ($liste_champs_table = mysql_fetch_array($result0)){
$array_of_attributs[$cpt] = $liste_champs_table[0];
$cpt ++;
}
for ($i = 0; $i < count($array_of_attributs)-1; $i++){
$newFileContent .= $array_of_attributs[$i].",";
}
$newFileContent .= $array_of_attributs[count($array_of_attributs)-1]."\n";
//Modification du fichier /N
$fileContent = file_get_contents($tempFile);
// Gestion des missing values
if (isset ($missingValue)){
if ($missingValue == 'period'){
$missVal = ",.,";
$missValFin = ",.";
}
if ($missingValue == 'nothing'){
$missVal = ",,";
$missValFin = ",";
}
if ($missingValue == 'blank'){
$missVal = ", ,";
$missValFin = ", ";
}
if ($missingValue == '-9999'){
$missVal = ",-9999,";
$missValFin = ",-9999";
}
} //fin du if isset $missingValue
if (file_exists($tempFile)){unlink($tempFile);}
if (file_exists($mysqlFile)){unlink($mysqlFile);}
$chercheChampsNull = ",\\N,";
//gestion des missing values
$remplaceChampsNull = $missVal;
$fileContent = str_replace($chercheChampsNull, $remplaceChampsNull, $fileContent);
$fileContent = str_replace($chercheChampsNull, $remplaceChampsNull, $fileContent);
$chercheChampsNullFin =",\\N";
$remplaceChampsNullFin = $missValFin;
$fileContent = str_replace($chercheChampsNullFin,$remplaceChampsNullFin,$fileContent);
$newFileContent .= $fileContent;
touch ($tempFile);
chmod ($tempFile, 0755);
if ($handle = fopen($tempFile,"w")){
$octets = fwrite($handle, $newFileContent);
if ($octets){
header("Content-disposition: attachment; filename=".$nameFile);
header("Content-Type: application/force-download");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($tempFile));
header("Pragma: no-cache");
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
header("Expires: 0");
readfile($nameFile);
}
else{echo "ERROR";}
fclose($handle);
}
else {echo "Le processus de creation de fichier a echoué";}
}//fin du (copy($mysqlFile, $tempFile))
//unlink($mysqlFile);
}//fin du if file exist
}//fin du isset $_POST download all
}//fin du isset DOWNLOAD = 1
}//fin du isset general
mysql_close($connectToDB);
?> |
Partager