SplFileObject

thierryR51 Messages postés 139 Date d'inscription   Statut Membre Dernière intervention   -  
thierryR51 Messages postés 139 Date d'inscription   Statut Membre Dernière intervention   -
Bonjour.
Je me suis inspiré de cette page:
 http://www.mon-code.net/article/49/lire-et-ecrire-facilement-des-fichiers-csv-avec-la-spl-de-php5


Je voudrais demander à Apache de créer un fichier et exporter des datas:

 $csv = new SplFileObject($fileCSV,'W+'); 


Oui mais voila, je bloque, ou du moins apache bloque là, et le fichier n'est pas créé. Pas de message d'erreur.

$fileCSV = /home/thierry/public_html/export.csv


Je présume que ça doit être un problème de droit. Mais comment gérer ça avec apache. Il me semblait que j'ai bien paramétré quand j'ai mis apache sur ma fedora. Y a t-il un moyen de vérifier tout ça?

Merci à tous.

6 réponses

jordane45 Messages postés 38486 Date d'inscription   Statut Modérateur Dernière intervention   4 752
 
Bonjour,

Déjà, il manque les guillemets autour du chemin et un point-virgule à la fin de la ligne.

Ensuite, tu peux vérifier, en php, si le chemin existe
https://www.php.net/manual/fr/function.is-dir.php
et si on peut y écrire :
https://www.php.net/manual/fr/function.is-writable.php

Il serait bien également, si ce n'est pas déjà fait, d'activer l'affichage des erreurs php:
//A placer au début du script
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);


PS: Je pense que pour le chemin, le chemin relatif à partir du répertoire racine de ton script suffit.
Donc :
$fileCSV = "export.csv";


0
thierryR51 Messages postés 139 Date d'inscription   Statut Membre Dernière intervention   8
 
Merci de ton aide Jordanne.
Voici un message d'erreur (enfin). J'ai corrigé la ponctuation...
Warning: Use of undefined constant chemin - assumed 'chemin' (this will throw an Error in a future version of PHP) in /home/thierry/public_html/php/asseleci/functions/log.php on line 25
export: construction du fichier '/home/thierry/public_html/export.csv'

Fatal error: Uncaught RuntimeException: SplFileObject::__construct('/home/thierry/public_html/export.csv'): failed to open stream: No such file or directory in /home/thierry/public_html/php/asseleci/functions/functiongeneral.php:126 Stack trace: #0 /home/thierry/public_html/php/asseleci/functions/functiongeneral.php(126): SplFileObject->__construct(''/home/thierry/...', 'w') #1 /home/thierry/public_html/php/asseleci/pages/portage/vue_liste.php(212): export() #2 {main} thrown in /home/thierry/public_html/php/asseleci/functions/functiongeneral.php on line 126


Mais comment résoudre ce truc ?

J'ai essayé de faire autrement en ne prenant aucun chemin:

Fatal error: Uncaught RuntimeException: SplFileObject::__construct(export.csv): failed to open stream: Permission denied in /home/thierry/public_html/php/asseleci/functions/functiongeneral.php:125 Stack trace: #0 /home/thierry/public_html/php/asseleci/functions/functiongeneral.php(125): SplFileObject->__construct('export.csv', 'w') #1 /home/thierry/public_html/php/asseleci/pages/portage/vue_liste.php(212): export() #2 {main} thrown in /home/thierry/public_html/php/asseleci/functions/functiongeneral.php on line 12
5
0
jordane45 Messages postés 38486 Date d'inscription   Statut Modérateur Dernière intervention   4 752
 
Dans voir ton code...
As tu mis un $ devant ta variable chemin ?
0
thierryR51 Messages postés 139 Date d'inscription   Statut Membre Dernière intervention   8
 
Grace à tes indications, j'ai pu effectuer des débugages intéressants. Le code est donc un peu modifié. Je te mets la fonction: Il y a beaucoup d'indicateurs car je cherche la solution.

 function export(){  //crée un fichier .CSV avec les éléments à partir de $sql passé par rewrite_var()
    debug("export DEBUT");
    
    $sql= $GLOBALS["sql"];   
    if(empty($sql)){
        jsalert('aucune donnee a exporter');
        return FALSE;
    }
    
    $DBexport= sqli_DB($sql);
    $T_info= mysqli_fetch_fields($DBexport);
           
    
    //print_r($T_info);
    
    // recherche du fichier à ouvrir en chmod 777 pour accès à php
   
//chemin(EXPORT_CSV);
     $fileCSV= $_SERVER['CONTEXT_DOCUMENT_ROOT'].'/'.EXPORT_CSV;
     
    if(!$fileCSV) {jsalert("Fichier $fileCSV est non trouvé: Le créer et lui donner les droits 777"); }
    
    $fileCSV="'".$fileCSV."'";
    debug("export: construction du fichier ".$fileCSV);  
    
    $csv = new SplFileObject($fileCSV,'w');  // voir http://www.mon-code.net/article/49/lire-et-ecrire-facilement-des-fichiers-csv-avec-la-spl-de-php5

    debug('SplFileObject');
    if(!$csv->valid) {
        jsalert('Objet SplFileObject non créé'); return FALSE;
    }
    foreach($T_info as $info) {
        // insertion des titres de colonnes
        echo $info->name.'</br>';
        $csv->fputcsv($info->name, ';');
    }
    
    // insertion du contenu des colonnes
    while($T_row= mysqli_fetch_row($DBexport)){
        $T_data[]= $T_row;
    }
    $csv->fputcsv($T_data, ';');    
    mysqli_free_result($DBexport);
    //Mise en forme
//   for($i=0;$i< mysql_num_fields($DBexport);$i++)  // field is numeric
 //      if(strpos($row[$i],'.')) $row[$i]=  str_replace('.',',',$row[$i]);
 
    jsalert("Exportation ".EXPORT_CSV." terminée.");
 
    debug("export ".EXPORT_CSV."  FIN");

    
}//function export()

La réponse actuelle est:
Fatal error: Uncaught RuntimeException: SplFileObject::__construct('/home/thierry/public_html/export.csv'): failed to open stream: No such file or directory in /home/thierry/public_html/php/asseleci/functions/functiongeneral.php:125 Stack trace: #0 /home/thierry/public_html/php/asseleci/functions/functiongeneral.php(125): SplFileObject->__construct(''/home/thierry/...', 'w') #1 /home/thierry/public_html/php/asseleci/pages/portage/vue_liste.php(214): export() #2 {main} thrown in /home/thierry/public_html/php/asseleci/functions/functiongeneral.php on line 125


Ne pas trop regarder après la ligne 26 car mon code bloque à la création de l'objet....


0
thierryR51 Messages postés 139 Date d'inscription   Statut Membre Dernière intervention   8
 
up !
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
thierryR51 Messages postés 139 Date d'inscription   Statut Membre Dernière intervention   8
 
Bon ! ben, je ne m'en suis pas sortis. Désolé. Bon Noël à tous.
0
thierryR51 Messages postés 139 Date d'inscription   Statut Membre Dernière intervention   8
 
J'ai enfin trouvé. La function tel que définie ne fonctionne pas.
J'ai ajouté avant l'appel de la SPL:
fopen($fileCSV,'w');    
$csv = new SplFileObject($fileCSV,'w');

et là ça fonctionne.
0