Erreurs avec fputs

Résolu
Tobi86 Messages postés 405 Date d'inscription   Statut Membre Dernière intervention   -  
ElementW Messages postés 4814 Date d'inscription   Statut Contributeur Dernière intervention   -
Bonsoir a toues, voilà je ne comprends pas pourquoi mon programme ne fonctionne pas ... Pourriez-vous m'éclairer ?

<?php
include("connexion.php");
$myfile="list-logs.php";

$req="SELECT DISTINCT id, name, first_name, mail, login, password FROM users_profs";
$res = $connect->query($req);
//var_dump($req);
$i=1;
   
 $fichier = 'chemin_du_fichier/fichier.txt';

   if( file_exists ( $myfile))
     unlink( $myfile );

//Traitement du résultat
while($row=$res->fetch(PDO::FETCH_ASSOC))
	{

		fputs($myfile, "$id".$i."=".$row['id']."\n");
		fputs($myfile, "$name".$i."=".$row['name']."\n");
		fputs($myfile, "$firstName".$i."=".$row['first_name']."\n");
		fputs($myfile, "$mail".$i."=".$row['mail']."\n");
		fputs($myfile, "$login".$i."=".$row['login']."\n");
		fputs($myfile, "$password".$i."=".$row['password']."\n");

		$i=$i+1;
	}
?>

2 réponses

ElementW Messages postés 4814 Date d'inscription   Statut Contributeur Dernière intervention   1 223
 
'lut,
fputs
prend un descripteur de fichier en premier paramètre, pas le nom du fichier. Donc il faut faire un
fopen
et un
fclose
pour que ça marche:
$myfile=fopen("list-logs.php", "w");
...
// Le reste de ton code
...
fclose($myfile);
0
Tobi86 Messages postés 405 Date d'inscription   Statut Membre Dernière intervention   57
 
PHP Warning:  file_exists() expects parameter 1 to be string, resource given in /home/thibaultc/Documents/www/appli/log/actua-list-logs.php on line 12
PHP Notice:  Undefined variable: id in /home/thibaultc/Documents/www/appli/log/actua-list-logs.php on line 19
PHP Notice:  Undefined variable: name in /home/thibaultc/Documents/www/appli/log/actua-list-logs.php on line 20
PHP Notice:  Undefined variable: firstName in /home/thibaultc/Documents/www/appli/log/actua-list-logs.php on line 21
PHP Notice:  Undefined variable: mail in /home/thibaultc/Documents/www/appli/log/actua-list-logs.php on line 22
PHP Notice:  Undefined variable: login in /home/thibaultc/Documents/www/appli/log/actua-list-logs.php on line 23
PHP Notice:  Undefined variable: password in /home/thibaultc/Documents/www/appli/log/actua-list-logs.php on line 24
PHP Notice:  Undefined variable: id in /home/thibaultc/Documents/www/appli/log/actua-list-logs.php on line 19
PHP Notice:  Undefined variable: name in /home/thibaultc/Documents/www/appli/log/actua-list-logs.php on line 20
PHP Notice:  Undefined variable: firstName in /home/thibaultc/Documents/www/appli/log/actua-list-logs.php on line 21
PHP Notice:  Undefined variable: mail in /home/thibaultc/Documents/www/appli/log/actua-list-logs.php on line 22
PHP Notice:  Undefined variable: login in /home/thibaultc/Documents/www/appli/log/actua-list-logs.php on line 23
PHP Notice:  Undefined variable: password in /home/thibaultc/Documents/www/appli/log/actua-list-logs.php on line 24
PHP Notice:  Undefined variable: id in /home/thibaultc/Documents/www/appli/log/actua-list-logs.php on line 19
PHP Notice:  Undefined variable: name in /home/thibaultc/Documents/www/appli/log/actua-list-logs.php on line 20
PHP Notice:  Undefined variable: firstName in /home/thibaultc/Documents/www/appli/log/actua-list-logs.php on line 21
PHP Notice:  Undefined variable: mail in /home/thibaultc/Documents/www/appli/log/actua-list-logs.php on line 22
PHP Notice:  Undefined variable: login in /home/thibaultc/Documents/www/appli/log/actua-list-logs.php on line 23
PHP Notice:  Undefined variable: password in /home/thibaultc/Documents/www/appli/log/actua-list-logs.php on line 24
PHP Notice:  Undefined variable: id in /home/thibaultc/Documents/www/appli/log/actua-list-logs.php on line 19
PHP Notice:  Undefined variable: name in /home/thibaultc/Documents/www/appli/log/actua-list-logs.php on line 20
PHP Notice:  Undefined variable: firstName in /home/thibaultc/Documents/www/appli/log/actua-list-logs.php on line 21
PHP Notice:  Undefined variable: mail in /home/thibaultc/Documents/www/appli/log/actua-list-logs.php on line 22
PHP Notice:  Undefined variable: login in /home/thibaultc/Documents/www/appli/log/actua-list-logs.php on line 23
PHP Notice:  Undefined variable: password in /home/thibaultc/Documents/www/appli/log/actua-list-logs.php on line 24
PHP Notice:  Undefined variable: id in /home/thibaultc/Documents/www/appli/log/actua-list-logs.php on line 19
PHP Notice:  Undefined variable: name in /home/thibaultc/Documents/www/appli/log/actua-list-logs.php on line 20
PHP Notice:  Undefined variable: firstName in /home/thibaultc/Documents/www/appli/log/actua-list-logs.php on line 21
PHP Notice:  Undefined variable: mail in /home/thibaultc/Documents/www/appli/log/actua-list-logs.php on line 22
PHP Notice:  Undefined variable: login in /home/thibaultc/Documents/www/appli/log/actua-list-logs.php on line 23
PHP Notice:  Undefined variable: password in /home/thibaultc/Documents/www/appli/log/actua-list-logs.php on line 24
PHP Notice:  Undefined variable: id in /home/thibaultc/Documents/www/appli/log/actua-list-logs.php on line 19
PHP Notice:  Undefined variable: name in /home/thibaultc/Documents/www/appli/log/actua-list-logs.php on line 20
PHP Notice:  Undefined variable: firstName in /home/thibaultc/Documents/www/appli/log/actua-list-logs.php on line 21
PHP Notice:  Undefined variable: mail in /home/thibaultc/Documents/www/appli/log/actua-list-logs.php on line 22
PHP Notice:  Undefined variable: login in /home/thibaultc/Documents/www/appli/log/actua-list-logs.php on line 23
PHP Notice:  Undefined variable: password in /home/thibaultc/Documents/www/appli/log/actua-list-logs.php on line 24

0
ElementW Messages postés 4814 Date d'inscription   Statut Contributeur Dernière intervention   1 223
 
Il faut que tu changes tes doubles guillemets en guillemets simples ('), sinon PHP essaie d'interpréter les variables dedans, d'où la masse d'erreurs.
Pour l'erreur du
file_exists()
; reviens à ton code précédent et changes-le comme ceci:
$myfilename="list-logs.php";
...
if( file_exists ( $myfilename))
    unlink( $myfilename );
$myfile=fopen("list-logs.php", "w");
...
// traitement
...
fclose($myfile);
0
Tobi86 Messages postés 405 Date d'inscription   Statut Membre Dernière intervention   57
 
Super ! Merci beaucoup ça fonctionne ! mais j'ai une dernière petite question, est ce que si je met mes
\n
entre double cottes, j'aurais mon retour à la ligne ? parce que actuellement ça m'écrit les données comme ceci :
$password1=63a9f0ea7bb98050796b649e85481845\n$id2=2
au lieu de :
$password1=63a9f0ea7bb98050796b649e85481845
$id2=2
0
ElementW Messages postés 4814 Date d'inscription   Statut Contributeur Dernière intervention   1 223
 
Oui, les escapes sequences sont aussi interprétées entre guillemets simples, dont
\n
. Les doubles guillemets permettent l'insertion facile de contenus de variable sans devoir faire
"".$var.""
à chaque fois.
0