par
jduv » 06 mars 2006, 12:24
Bonjour et merci pour la réponse,
J'ai inséré les headers comme suis :
if($fp = fopen("fichier_temp/".$fichier.".csv","a")){ //ouverture du fichier en ecriture
fputs($fp, ereg_replace(",","\t","date_j,".$param.$vent.$hum.$code.$instant.$autre."id_mesure_journ\n"));
while ($tab=pg_fetch_array($query,NULL,PGSQL_BOTH))
{
for ($i=0;$i<$nb_param;$i++)
{
if ($i==$nb_param-1) {fputs($fp, "$tab[$i]");} else {fputs($fp, "$tab[$i]\t");}
}
fputs($fp, "\n");
}
header("Content-type: application/force-download");
header("Content-Disposition: attachment; filename=".$fichier);
readfile("fichier_temp/".$fichier);
fclose($fp);
echo "ok";
}else{ // L ouverture a echouee
echo "Ouverture du fichier impossible";
exit();
}
Avec
$fichier étant le nom choisi via le formulaire appelant.
Mon probléme est que le fichier écrit sur mon serveur contient bien le résultat de ma requete SQL mais le fichier téléchargé, lui ne contient rien sur ma requête SQL mais prend bien le nom choisi.?
Désolé si mon code n'est pas un modéle du genre mais quant on débute...
Merci d'avance
Ci dessous le code complet de ma page (peut etre pas necessaire)
<?php
session_start();
if(!isset($_SESSION['login'])) {
echo "Vous n'etes pas autoriser a acceder a cette zone !!";
header('Location:index.php');
exit;
}
$login =$_SESSION['login'];
$password = $_SESSION['password'];
$nom = $_SESSION['nom'];
$prenom=$_SESSION['prenom'];
$db =pg_connect("host=localhost dbname=Base_test_LRM user=$login password=$password")
or die ("Echec sur pg_connect".pg_last_error());
$num_poste=$_SESSION['num_poste'];
$type_fichier = $_POST['type_fichier'];
//echo "Type fichier : ".$type_fichier."<br/>";
$fichier = $_POST['fichier'];
//echo "fichier : ".$fichier."<br/>";
//RECUPERATION DES CHOIX DE LA LISTE PARAM
$choix_param = $_POST['param'];
$param = "";
for ($i=0; $i<count($choix_param); $i++) {
if ($i==count($choix_param)-1) {$param=$param."$choix_param[$i],";} else {$param=$param."$choix_param[$i],";}
}
//echo $param."<br>";
//RECUPERATION DES CHOIX DE LA LISTE CODE
$choix_code = $_POST['code'];
$code = "";
for ($i=0; $i<count($choix_code); $i++) {
if ($i==count($choix_code)-1) {$code=$code."$choix_code[$i],";} else {$code=$code."$choix_code[$i],";}
}
//echo $code."<br>";
//RECUPERATION DES CHOIX DE LA LISTE VENT
$choix_vent = $_POST['vent'];
$vent = "";
for ($i=0; $i<count($choix_vent); $i++) {
if ($i==count($choix_vent)-1) {$vent=$vent."$choix_vent[$i],";} else {$vent=$vent."$choix_vent[$i],";}
}
//echo $vent."<br>";
//RECUPERATION DES CHOIX DE LA LISTE HUM
$choix_hum = $_POST['hum'];
$hum = "";
for ($i=0; $i<count($choix_hum); $i++) {
if ($i==count($choix_hum)-1) {$hum=$hum."$choix_hum[$i],";} else {$hum=$hum."$choix_hum[$i],";}
}
//echo $hum."<br>";
//RECUPERATION DES CHOIX DE LA LISTE INSTANT
$choix_instant = $_POST['instant'];
$instant = "";
for ($i=0; $i<count($choix_instant); $i++) {
if ($i==count($choix_instant)-1) {$instant=$instant."$choix_instant[$i],";} else {$instant=$instant."$choix_instant[$i],";}
}
//echo $instant."<br>";
//RECUPERATION DES CHOIX DE LA LISTE AUTRE
$choix_autre = $_POST['autre'];
$autre = "";
for ($i=0; $i<count($choix_autre); $i++) {
if ($i==count($choix_autre)-1) {$autre=$autre."$choix_autre[$i],";} else {$autre=$autre."$choix_autre[$i],";}
}
//echo $autre."<br>";
//COMPTE LE NOMBRE DE PARAMETRES SELECTIONNES
$nb_param =2+count($choix_vent)+count($choix_hum)+count($choix_code)+count($choix_instant)+count($choix_autre)+count($choix_param);
$sql =$_POST['sql'];
$jour_debut = $_POST['jour_debut'];
$mois_debut = $_POST['mois_debut'];
$annee_debut = $_POST['annee_debut'];
$jour_fin = $_POST['jour_fin'];
$mois_fin = $_POST['mois_fin'];
$annee_fin = $_POST['annee_fin'];
$date_debut = $annee_debut."-".$mois_debut."-".$jour_debut;
$date_fin = $annee_fin."-".$mois_fin."-".$jour_fin;
$requete_code = ("SELECT * FROM t_aide_code_meteo WHERE attribut = ".$_POST['amp']."");
//echo $requete_code."<br>";
$requete = ("SELECT date_j,".$param.$vent.$hum.$code.$instant.$autre."id_mesure_journ FROM t_meteo_journaliere WHERE num_poste=".$num_poste." AND date_j BETWEEN '".$date_debut."' AND '".$date_fin."' ORDER BY date_j");
$query = pg_query($db,$requete) or die ("Echec sur pg_query".pg_last_error($db)."SELECT date_j,".$param.$vent.$hum.$code.$instant.$autre."id_mesure_journ FROM t_meteo_journaliere WHERE num_poste=".$num_poste." AND date_j BETWEEN '".$date_debut."' AND '".$date_fin."' ORDER BY date_j");;
//echo $requete."<br/>";
//echo "Nombre de parametres :".$nb_param;
//recuperation du nombre d'enregistrement de la requete
$nb_enreg = pg_num_rows($query);
//echo "<br/>Nombre de lignes : ".$nb_enreg."<br/>";
if($fp = fopen("fichier_temp/".$fichier.".csv","a")){ //ouverture du fichier en ecriture
fputs($fp, ereg_replace(",","\t","date_j,".$param.$vent.$hum.$code.$instant.$autre."id_mesure_journ\n"));
while ($tab=pg_fetch_array($query,NULL,PGSQL_BOTH))
{
for ($i=0;$i<$nb_param;$i++)
{
if ($i==$nb_param-1) {fputs($fp, "$tab[$i]");} else {fputs($fp, "$tab[$i]\t");}
}
fputs($fp, "\n");
}
header("Content-type: application/force-download");
header("Content-Disposition: attachment; filename=".$fichier);
readfile("fichier_temp/".$fichier);
fclose($fp);
//echo "ok";
}else{ // L ouverture a echouee
echo "Ouverture du fichier impossible";
exit();
}
?>
Bonjour et merci pour la réponse,
J'ai inséré les headers comme suis :
[php]if($fp = fopen("fichier_temp/".$fichier.".csv","a")){ //ouverture du fichier en ecriture
fputs($fp, ereg_replace(",","\t","date_j,".$param.$vent.$hum.$code.$instant.$autre."id_mesure_journ\n"));
while ($tab=pg_fetch_array($query,NULL,PGSQL_BOTH))
{
for ($i=0;$i<$nb_param;$i++)
{
if ($i==$nb_param-1) {fputs($fp, "$tab[$i]");} else {fputs($fp, "$tab[$i]\t");}
}
fputs($fp, "\n");
}
header("Content-type: application/force-download");
header("Content-Disposition: attachment; filename=".$fichier);
readfile("fichier_temp/".$fichier);
fclose($fp);
echo "ok";
}else{ // L ouverture a echouee
echo "Ouverture du fichier impossible";
exit();
}[/php]
Avec[b] $fichier[/b] étant le nom choisi via le formulaire appelant.
Mon probléme est que le fichier écrit sur mon serveur contient bien le résultat de ma requete SQL mais le fichier téléchargé, lui ne contient rien sur ma requête SQL mais prend bien le nom choisi.?
Désolé si mon code n'est pas un modéle du genre mais quant on débute...
Merci d'avance
Ci dessous le code complet de ma page (peut etre pas necessaire)
[php]<?php
session_start();
if(!isset($_SESSION['login'])) {
echo "Vous n'etes pas autoriser a acceder a cette zone !!";
header('Location:index.php');
exit;
}
$login =$_SESSION['login'];
$password = $_SESSION['password'];
$nom = $_SESSION['nom'];
$prenom=$_SESSION['prenom'];
$db =pg_connect("host=localhost dbname=Base_test_LRM user=$login password=$password")
or die ("Echec sur pg_connect".pg_last_error());
$num_poste=$_SESSION['num_poste'];
$type_fichier = $_POST['type_fichier'];
//echo "Type fichier : ".$type_fichier."<br/>";
$fichier = $_POST['fichier'];
//echo "fichier : ".$fichier."<br/>";
//RECUPERATION DES CHOIX DE LA LISTE PARAM
$choix_param = $_POST['param'];
$param = "";
for ($i=0; $i<count($choix_param); $i++) {
if ($i==count($choix_param)-1) {$param=$param."$choix_param[$i],";} else {$param=$param."$choix_param[$i],";}
}
//echo $param."<br>";
//RECUPERATION DES CHOIX DE LA LISTE CODE
$choix_code = $_POST['code'];
$code = "";
for ($i=0; $i<count($choix_code); $i++) {
if ($i==count($choix_code)-1) {$code=$code."$choix_code[$i],";} else {$code=$code."$choix_code[$i],";}
}
//echo $code."<br>";
//RECUPERATION DES CHOIX DE LA LISTE VENT
$choix_vent = $_POST['vent'];
$vent = "";
for ($i=0; $i<count($choix_vent); $i++) {
if ($i==count($choix_vent)-1) {$vent=$vent."$choix_vent[$i],";} else {$vent=$vent."$choix_vent[$i],";}
}
//echo $vent."<br>";
//RECUPERATION DES CHOIX DE LA LISTE HUM
$choix_hum = $_POST['hum'];
$hum = "";
for ($i=0; $i<count($choix_hum); $i++) {
if ($i==count($choix_hum)-1) {$hum=$hum."$choix_hum[$i],";} else {$hum=$hum."$choix_hum[$i],";}
}
//echo $hum."<br>";
//RECUPERATION DES CHOIX DE LA LISTE INSTANT
$choix_instant = $_POST['instant'];
$instant = "";
for ($i=0; $i<count($choix_instant); $i++) {
if ($i==count($choix_instant)-1) {$instant=$instant."$choix_instant[$i],";} else {$instant=$instant."$choix_instant[$i],";}
}
//echo $instant."<br>";
//RECUPERATION DES CHOIX DE LA LISTE AUTRE
$choix_autre = $_POST['autre'];
$autre = "";
for ($i=0; $i<count($choix_autre); $i++) {
if ($i==count($choix_autre)-1) {$autre=$autre."$choix_autre[$i],";} else {$autre=$autre."$choix_autre[$i],";}
}
//echo $autre."<br>";
//COMPTE LE NOMBRE DE PARAMETRES SELECTIONNES
$nb_param =2+count($choix_vent)+count($choix_hum)+count($choix_code)+count($choix_instant)+count($choix_autre)+count($choix_param);
$sql =$_POST['sql'];
$jour_debut = $_POST['jour_debut'];
$mois_debut = $_POST['mois_debut'];
$annee_debut = $_POST['annee_debut'];
$jour_fin = $_POST['jour_fin'];
$mois_fin = $_POST['mois_fin'];
$annee_fin = $_POST['annee_fin'];
$date_debut = $annee_debut."-".$mois_debut."-".$jour_debut;
$date_fin = $annee_fin."-".$mois_fin."-".$jour_fin;
$requete_code = ("SELECT * FROM t_aide_code_meteo WHERE attribut = ".$_POST['amp']."");
//echo $requete_code."<br>";
$requete = ("SELECT date_j,".$param.$vent.$hum.$code.$instant.$autre."id_mesure_journ FROM t_meteo_journaliere WHERE num_poste=".$num_poste." AND date_j BETWEEN '".$date_debut."' AND '".$date_fin."' ORDER BY date_j");
$query = pg_query($db,$requete) or die ("Echec sur pg_query".pg_last_error($db)."SELECT date_j,".$param.$vent.$hum.$code.$instant.$autre."id_mesure_journ FROM t_meteo_journaliere WHERE num_poste=".$num_poste." AND date_j BETWEEN '".$date_debut."' AND '".$date_fin."' ORDER BY date_j");;
//echo $requete."<br/>";
//echo "Nombre de parametres :".$nb_param;
//recuperation du nombre d'enregistrement de la requete
$nb_enreg = pg_num_rows($query);
//echo "<br/>Nombre de lignes : ".$nb_enreg."<br/>";
if($fp = fopen("fichier_temp/".$fichier.".csv","a")){ //ouverture du fichier en ecriture
fputs($fp, ereg_replace(",","\t","date_j,".$param.$vent.$hum.$code.$instant.$autre."id_mesure_journ\n"));
while ($tab=pg_fetch_array($query,NULL,PGSQL_BOTH))
{
for ($i=0;$i<$nb_param;$i++)
{
if ($i==$nb_param-1) {fputs($fp, "$tab[$i]");} else {fputs($fp, "$tab[$i]\t");}
}
fputs($fp, "\n");
}
header("Content-type: application/force-download");
header("Content-Disposition: attachment; filename=".$fichier);
readfile("fichier_temp/".$fichier);
fclose($fp);
//echo "ok";
}else{ // L ouverture a echouee
echo "Ouverture du fichier impossible";
exit();
}
?>[/php]